Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

algorithm for magic no. #54

Merged
merged 1 commit into from
Oct 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Miscellaneous/MagicNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Java program to find nth
// magic numebr
import java.io.*;

class MagicNumber
{
// Function to find nth magic number
static int nthMagicNo(int n)
{
int pow = 1, answer = 0;

// Go through every bit of n
while (n != 0)
{
pow = pow*5;

// If last bit of n is set
if ((int)(n & 1) == 1)
answer += pow;

// proceed to next bit
// or n = n/2
n >>= 1;
}
return answer;
}

// Driver program to test
// above function
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a no.");
int n = Integer.parseInt(br.readLine());

System.out.println(n+"th magic" +
" number is " + nthMagicNo(n));
}
}


// This code is contributed by
// prerna saini