File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments