Skip to content

Commit

Permalink
Added code for nth magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
mahis929 committed Oct 9, 2018
1 parent 26efe80 commit 76db08d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions BitManipulation/Nth_magic_number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MAGIC NUMBER

A magic number is defined as a number which can be expressed as a power of 5 or sum of unique powers of 5. First few magic numbers are 5, 25, 30(5 + 25), 125, 130(125 + 5), ….
25 changes: 25 additions & 0 deletions BitManipulation/Nth_magic_number/nth_magic_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;

int nthMagicNo(int n)
{
int pow = 1, answer = 0;

while (n)
{
pow = pow*5;

if (n & 1)
answer += pow;

}
return answer;
}

int main()
{
int n;
cin>>n;
cout << "nth magic number is " << nthMagicNo(n) << endl;
return 0;
}

0 comments on commit 76db08d

Please sign in to comment.