Algorithm C Programming Coding Mathematical Algorithms

C programming-Lucky Numbers

C programming-Lucky Numbers - Mathematical algorithms - Lucky numbers are subset of integers. let us see the process of arriving at lucky numbers.

Lucky numbers are subset of integers. Rather than going into much theory, let us see the process of arriving at lucky numbers,

Take the set of integers

First, delete every second number, we get following reduced set.

Now, delete every third number, we get
1, 3, 7, 9, 13, 15, 19,….….

Continue this process indefinitely……
Any number that does NOT get deleted due to above process is called “lucky”.

Therefore, set of lucky numbers is 1, 3, 7, 13,………

Now, given an integer ‘n’, write a function to say whether this number is lucky or not.

    bool isLucky(int n)

Before every iteration, if we calculate position of the given no, then in a given iteration, we can determine if the no will be deleted. Suppose calculated position for the given no. is P before some iteration, and each Ith no. is going to be removed in this iteration, if P < I then input no is lucky, if P is such that P%I == 0 (I is a divisor of P), then input no is not lucky.

Recursive Way:

#include <stdio.h>
#define bool int
/* 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 1;
  if(n%counter == 0)
    return 0;      
 /*calculate next position of input no*/
  next_position -= next_position/counter;
  return isLucky(next_position);
/*Driver function to test above function*/
int main()
  int x = 5;
  if( isLucky(x) )
    printf("%d is a lucky no.", x);
    printf("%d is not a lucky no.", x);

In next step every 6th no .in sequence will be deleted. 19 will not be deleted after this step because position of 19 is 5th after this step. Therefore, 19 is lucky.

READ  C Programming - Reservoir Sampling

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment