Lucky Numbers | Practice | GeeksforGeeks

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
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”.
Example 1:
Input:
N = 5
Output: 0
Explanation: 5 is not a lucky number 
as it gets deleted in the second 
iteration.

Example 2:
Input:
N = 19
Output: 1
Explanation: 19 is a lucky number

Your Task:
You don't need to read input or print anything. You only need to complete the function isLucky() that takes N as parameter and returns either False if the N is not lucky else True.
Expected Time Complexity: O(sqrt(N)).
Expected Auxiliary Space: O(sqrt(N)).
Constraints:
1 <= N <= 105
0
sunilnishad100006 days ago
*******C++ Code*********
bool yesorno(int n, int r){   if(r<=n){       if(n%r==0){           return 0;       }else {       return yesorno(n-n/r,r+1);       }   }else {       return 1;   }   }
bool isLucky(int n) {   yesorno(n,2);}
+1
rahulrs1 week ago
Java code................................

public static boolean isLucky(int n)
    {
        // Your code here
        boolean ans = check(n,2);
        return ans;
        
    }
    public static boolean check(int n, int counter){
        
        if(counter>n) return true;
        if(n%counter==0) return false;
        
        return check(n-(n/counter), counter+1);
        
    }
0
akshatsaxenaanpas1 week ago
bool checklucky(int n, int counter){
    if(n%counter==0){
        return false;
    }
    if(counter>n){
        return true;
    }
    
    n = n - n/counter;
    counter = counter+1;
    
    return checklucky(n,counter);
}
bool isLucky(int n) {
    // code here
    bool t = checklucky(n,2);
     
    return t;
}

0
crawler2 weeks ago
Approach: We Check for a given number n, the position of its appearance in each step,
example: in first step 7 appears at 7th position and i=2, but 7%2 ≠ 0, so recur to next stage modifying the position of 7 for the next step:
Calculate position: position can be calculated by subtracting number of multiples of 2(i in previous state) less than n from n: i.e. n = n-(n/i), where n/i is number of numbers divisible by i.
bool myFun(int n, int x){
    if(n%x == 0)
        return false;
    if(x > n)
        return true;
    return myFun(n-(n/x), x+1);
}
bool isLucky(int n) {
    return myFun(n, 2);
}
0
sushmithaguggella
This comment was deleted.
+3
zakarian17s3 weeks ago
In the Question it is not made clear how this process should run, whether like deleting 2nd then 3rd then 4th then 5th ............. like this or deleting 2nd then 3rd then 2nd then 3rd .......... like this pls make this clear @gfg
+1
nihal_singh1841 month ago
why this code is not working for 74
bool isLucky(int n) {   static int k=2;   if(k>n){       return true;   }   else if(n%k==0){       return false;   }   return isLucky(n-(n/(k++)));   // else{   //     int nextpos=n-(n/k);   //     k++;   //     return isLucky(nextpos);   
0
ankitparashxr1 month ago
java
 public static boolean isLucky(int n)   {       ArrayList<Integer> num = new ArrayList<>();       for(int i=1;i<=n;i++)       {           if(i%2!=0)           {               num.add(i);           }       }       //System.out.println(num);       ArrayList<Integer> num1 =lucky(num,1,3);       if(num1.contains(n))       {           return true;       }       return false;   }   public static ArrayList<Integer> lucky(ArrayList<Integer> num,int a,int b)    {       if(num.size()<b)       {           return num;       }       ArrayList<Integer> num1 = new ArrayList<>();       for(int i=0;i<num.size();i++,a++)       {          if(a!=b)          {              num1.add(num.get(i));          }          else          {              a=0;          }       }       return lucky(num1,1,b+1);   }}
0
jahidulhossainmekat1 month ago
    # Function attribute will act
    # as static variable  
def isLucky(n):

    if isLucky.counter > n:
        return 1
    if n % isLucky.counter == 0:
        return 0
    #calculate next position of input no.
    #Variable "next_position" is just for
    #readability of the program we can
    #remove it and update in "n" only
    next_position = n - (n/isLucky.counter)
     
    isLucky.counter = isLucky.counter + 1
     
    return isLucky(next_position)     
# Driver Code
 
isLucky.counter = 2 # Acts as static variable
x = 5
if isLucky(x):
    print (x,"is a Lucky number")
else:
    print (x,"is not a Lucky number")
# } Driver Code Ends
+2
mohitraj27411 month ago
Note that the position matters and not the actual number.
keep passing the new position with updated counter and check if its a lucky position in each pass.
 
bool Lucky(int n,int counter){   if(counter>n)   return true;   if(n%counter == 0)   return false;      return Lucky(n - floor(n/counter), counter+1);}
bool isLucky(int n) {   // code here   bool ans=Lucky(n,2);   return ans;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.