Lucky numbers are a subset of integers. Rather than going into much theory, let us see the process of arriving at lucky numbers:
Take the set of integers
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,……
First, delete every second number, we get following reduced set.
1,3,5,7,9,11,13,15,17,19,…………
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,………
Given an integer n, write a function to say whether this number is lucky or not.
Examples:
Input: n = 7
Output: 7 is a lucky number
Input: n = 9
Output: 9 is not a lucky number
To solve the problem follow the below idea:
Before every iteration, if we calculate the position of the given number, then in a given iteration, we can determine if the number will be deleted. Suppose calculated position for the given number is P before some iteration, and each ith number is going to be removed in this iteration, i
if P < i then input number is lucky,
if P is such that P%i == 0 (i is a divisor of P), then input no is not lucky.
How to calculate the Next position of the number:
We know that initially the position of the number is nth itself. Now any next position will be equal to the previous position minus the number of elements (or say items) removed.
That is, next_position = current_position – count of numbers removed
For example, take the case of n=13.
We have: Initial position: n, ie. 13 itself.
1,2,3,4,5,6,7,8,9,10,11,12,13
Now after removing every second elements , we actually removed n/2 elements. So now the position of 13 will be : n-n/2=13-6=7 (n=13), i=2
1,3,5,7,9,11,13.
After that, we remove n/3 items. Note that n now is n=7. So position of 13 : n-n/3 = 7-7/3 = 7-2 = 5 (n=7), i=3
1,3,7,9,13
So next it will be : n-n/4 = 5-5/4 = 4 (n=5), i=4
1,3,7,13
So now i=5, but since position of 13 is 4 only, so it will be saved. Hence a lucky number! n=4, i=5
Below is the recursive implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isLucky( int n)
{
static int counter = 2;
if (counter > n)
return 1;
if (n % counter == 0)
return 0;
int next_position = n - (n / counter);
counter++;
return isLucky(next_position);
}
int main()
{
int x = 5;
if (isLucky(x))
cout << x << " is a lucky no." ;
else
cout << x << " is not a lucky no." ;
}
|
C
#include <stdio.h>
#define bool int
bool isLucky( int n)
{
static int counter = 2;
if (counter > n)
return 1;
if (n % counter == 0)
return 0;
int next_position = n - (n / counter);
counter++;
return isLucky(next_position);
}
int main()
{
int x = 5;
if (isLucky(x))
printf ( "%d is a lucky no." , x);
else
printf ( "%d is not a lucky no." , x);
getchar ();
}
|
Java
import java.io.*;
class GFG {
public static int counter = 2 ;
static boolean isLucky( int n)
{
if (counter > n)
return true ;
if (n % counter == 0 )
return false ;
int next_position = n - (n / counter);
counter++;
return isLucky(next_position);
}
public static void main(String[] args)
{
int x = 5 ;
if (isLucky(x))
System.out.println(x + " is a lucky no." );
else
System.out.println(x + " is not a lucky no." );
}
}
|
Python
def isLucky(n):
if isLucky.counter > n:
return 1
if n % isLucky.counter = = 0 :
return 0
next_position = n - (n / isLucky.counter)
isLucky.counter = isLucky.counter + 1
return isLucky(next_position)
isLucky.counter = 2
x = 5
if isLucky(x):
print x, "is a Lucky number"
else :
print x, "is not a Lucky number"
|
C#
using System;
class GFG {
public static int counter = 2;
static bool isLucky( int n)
{
if (counter > n)
return true ;
if (n % counter == 0)
return false ;
int next_position = n - (n / counter);
counter++;
return isLucky(next_position);
}
public static void Main()
{
int x = 5;
if (isLucky(x))
Console.Write(x + " is a "
+ "lucky no." );
else
Console.Write(x + " is not"
+ " a lucky no." );
}
}
|
PHP
<?php
function isLucky( $n )
{
$counter = 2;
if ( $counter > $n )
return 1;
if ( $n % $counter == 0)
return 0;
$next_position = $n - ( $n / $counter );
$counter ++;
return isLucky( $next_position );
}
$x = 5;
if (isLucky( $x ) )
echo $x , " is a lucky no." ;
else
echo $x , " is not a lucky no." ;
?>
|
Javascript
<script>
function isLucky(n)
{
let counter = 2;
if (counter > n)
return 1;
if (n % counter == 0)
return 0;
let next_position = n - Math.floor(n/counter);
counter++;
return isLucky(next_position);
}
let x = 5;
if ( isLucky(x) )
document.write(x + " is a lucky no." );
else
document.write(x + " is not a lucky no." );
</script>
|
Output5 is not a lucky no.
Time Complexity: O(n)
Auxiliary Space: O(1)
Dry run of the above approach:
Let’s us take an example of 19
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,17,18,19,20,21,……
1,3,5,7,9,11,13,15,17,19,…..
1,3,7,9,13,15,19,……….
1,3,7,13,15,19,………
1,3,7,13,19,………
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. Let’s see how above C code finds out:
Current function call Position after this call Counter for next call Next Call isLucky(19 ) 10 3 isLucky(10) isLucky(10) 7 4 isLucky(7) isLucky(7) 6 5 isLucky(6) isLucky(6) 5 6 isLucky(5)
When isLucky(6) is called, it returns 1 (because counter > n).
Please write comments if you find any bugs in the given programs or other ways to solve the same problem.