Skip to content

Commit c04b3c5

Browse files
authored
lucky numbers in c++
1 parent 8a056d2 commit c04b3c5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

math/lucky_numbers.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Lucky number implementation
2+
// Carlos Abraham Hernandez
3+
// algorithms.abranhe.com/math/lucky-numbers
4+
5+
#include <stdio.h>
6+
#define bool int
7+
8+
/* Returns 1 if n is a lucky no. ohterwise returns 0*/
9+
bool isLucky(int n)
10+
{
11+
static int counter = 2;
12+
13+
/*variable next_position is just for readability of
14+
the program we can remove it and use n only */
15+
int next_position = n;
16+
if(counter > n)
17+
return 1;
18+
if(n%counter == 0)
19+
return 0;
20+
21+
/*calculate next position of input no*/
22+
next_position -= next_position/counter;
23+
24+
counter++;
25+
return isLucky(next_position);
26+
}
27+
28+
/*Driver function to test above function*/
29+
int main()
30+
{
31+
int x = 7;
32+
if( isLucky(x) )
33+
printf("%d is a lucky number.", x);
34+
else
35+
printf("%d is not a lucky number.", x);
36+
getchar();
37+
}

0 commit comments

Comments
 (0)