-
Notifications
You must be signed in to change notification settings - Fork 2
/
nth_magic_num.cpp
55 lines (39 loc) · 897 Bytes
/
nth_magic_num.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/****************************************************************************
File name: nth_magic_num.cpp
Author: babajr
*****************************************************************************/
/*
Program to get the nth magic number.
Magic Number:
input output
1 (0001) 5
2 (0010) 25
3 (0011) 25 + 5 = 30
4 (0100) 125
5 (0101) 130
*/
#include<bits/stdc++.h>
using namespace std;
/*
API to get the nth magic number.
*/
int nth_magic_num(int n)
{
int magic_number = 0;
int base = 5;
while(n > 0)
{
// get the last bit and right shift n by 1.
int last_bit = n & 1;
n = n >> 1;
magic_number = magic_number + (base * last_bit);
base = base * 5;
}
return magic_number;
}
int main(void)
{
int n = 3;
printf("nth Magic Number: %d\n", nth_magic_num(n));
return 0;
}