forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky_numbers.cpp
38 lines (31 loc) · 886 Bytes
/
lucky_numbers.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
// Lucky number implementation
// Carlos Abraham Hernandez
// algorithms.abranhe.com/math/lucky-numbers
#include <stdio.h>
#include <math.h>
/* Returns 1 if n is a lucky no. ohterwise returns 0*/
bool isLucky(int n)
{
static int counter = 2;
/*variable next_position is just for readability of
the program we can remove it and use n only */
int next_position = n;
if(counter > n)
return true;
if(n % counter == 0)
return false;
/*calculate next position of input no*/
next_position -= floor(next_position / counter);
counter++;
return isLucky(next_position);
}
/*Driver function to test above function*/
int main()
{
int x = 7;
if( isLucky(x) )
printf("%d is a lucky number.", x);
else
printf("%d is not a lucky number.", x);
getchar();
}