Skip to content

Commit cc86336

Browse files
Merge pull request #459 from mahis929/new_branch
Removed infinite loop from nth magic no.
2 parents 26efe80 + fbee54a commit cc86336

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# MAGIC NUMBER
2+
3+
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), ….
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int nthMagicNo(int n)
5+
{
6+
int pow = 1, answer = 0;
7+
8+
9+
while (n)
10+
{
11+
pow = pow*5;
12+
13+
14+
if (n & 1)
15+
answer += pow;
16+
17+
18+
n >>= 1;
19+
}
20+
return answer;
21+
}
22+
23+
int main()
24+
{
25+
int n;
26+
cin>>n;
27+
cout << "nth magic number is " << nthMagicNo(n) << endl;
28+
return 0;
29+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ACM-ICPC Algorithms is a collection of important algorithms and data structures
3131
* [Depth First Search](/DFS)
3232
* [Bit Manipulation](/BitManipulation)
3333
* [Checking Power of 2](/BitManipulation/Checking_power_of_2)
34+
* [Nth Magic No](/BitManipulation/Nth_magic_number)
3435
* [Count Ones](/BitManipulation/count_ones)
3536
* [Divide Integers](/BitManipulation/divide_integers)
3637
* [Even Odd](/BitManipulation/even_odd)

0 commit comments

Comments
 (0)