Lucky Number Program In Python


In this section, you are going to learn how to create the lucky number program in Python. You will look at 2 different ways to find a lucky number before N. Additionally, you will also learn to tell whether a number is lucky or not with the program.

    Table Of Contents

  1. Lucky number in Python
  2. Find Lucky Numbers Before N
    1. Method 1
    2. Method 2
  3. Program To Check If Number Is Lucky Or Not
  4. Conclusion

Lucky number in Python

In Number System, Lucky Number is a subset of natural numbers that you can find by following the following rules:

  1. Take a series of natural numbers from 1 to N. Consider the sequence be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
  2. Now, remove every second digit. The remaining numbers in the series left are 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, ...
  3. Now remove every third digit from the current series. The remaining numbers in the series left are 1, 3, 7, 9, 13, 15, 19, ...
  4. Now remove every fourth digit from the current series. The remaining numbers in the series left are 1, 3, 7, 13, 15, 19, ...
  5. Now remove every fifth digit from the current series. The remaining numbers in the series left are 1, 3, 7, 13, 19, ...
  6. No numbers can be removed now as no sixth number exist in the series so 1, 3, 7, 13, 19, ... becomes a lucky number.

Look at the image below to understand the above rules:

lucky number
Lucky Numbers

Find Lucky Numbers Before N

You have been introduced to what is a lucky number in the previous section. Now, you are going to create a program to find all the lucky numbers before N.

Let's now create a program in Python to find all lucky numbers before N (given number).


Method 1: Lucky Number Program In Python

We will start with taking number input from the user and create a python list and fill it with all the natural numbers from 1 to number.

After that, we will create a for loop that will run from 2 to half of the number. Now, in each execution of the loop, the program will check the step (the first time it is 2), and replace the list element falling on that step with 0. Another counter variable inside the loop will count the number of elements that are not 0. And step will convert the element value to 0 to only those elements that are not 0.

Here is the complete program using this approach:

# python program to find all lucky numbers before N

# take number input
num = int(input("Enter number: "))

# create list and fill it with numbers from 1 to num
arr = [i+1 for i in range(num)]

# loop to find the lucky number
for step in range(2, num//2):
    count = 0
    for i in range(num):
        # count is used to get the correct non-zero element
        if arr[i] != 0:
            count += 1
        # if count is divisible by step, then change the element to 0
        if count % step == 0:
            arr[i] = 0

# print the lucky number
for i in range(num):
    if arr[i] != 0:
        print(arr[i], end=" ")
print()

Output:

Enter number: 10
1 3 7
Enter number: 50
1 3 7 13 19 27 39 49

Code Explanation:

  1. First, take the number input from the user.
  2. Create a list and fill it with numbers from 1 to num.
  3. Start a loop to find lucky number that run from 2 to half of the number. This will be our 'step' in the loop.
  4. Inside the loop create a variable count. This variable will count those numbers in the series that are not 0 and the step will convert those elements to 0 that fall on that step.
  5. Check if the count is divisible by step. If it is then set the element to 0.
  6. Finally, print the non-zero elements of the list, these are our lucky numbers.

Method 2: Lucky Number Program In Python

In the second method, we will follow some different rules. Instead of changing the element to 0, we will shift the numbers to left by step value one by one. Where the step is 1, then 2, then 2, and so on.

First, we will shift the number to left by 1. Then we will shift the number to left by 2. Then we will shift the number to left by 3. And so on.

Here is the python program using this approach:

# python program to find all lucky numbers before N

# take input from the user
num = int(input("Enter the number: "))

# initialize array
arr = [i for i in range(1, num + 1)]

# find lucky number
step = 1
while (step < num) :
    i = step
    while (i < num) :
        j = i
        while (j < num - 1) :
            arr[j] = arr[j + 1]
            j += 1
        num -= 1
        i += step
    step += 1

# print array
for i in range(num):
    print(arr[i], end=" ")
print()

Output:

Enter number: 10
1 3 7
Enter number: 100
1 3 7 13 19 27 39 49 63 79 91

Program to Check if Number is Lucky or Not

Checking whether a number is lucky or not is quite a tricky task. We will create a python program to check if a number is lucky or not.

What approach will you take to find whether a number is a lucky number or not?🤔

Take your time⏳ and think about it.

Here is a brief explanation of how you can approach this problem:

Let the number to check be 7 and Consider the following series: 1, 2, 3, 4, 5, 6, 7. Position of number 7 is 7th in the series.

Remove every 2nd element from the series in the first step. After the first step, we get 1, 3, 5, 7. So the position of 7 is 4th which is 7 - floor(7/2) = previous_position - floor(previous_position/2).

Again after the next step, that is the removal of every 3rd element, we get 1, 3, 7. So the position of 7 is 3rd which is 4 - floor(4/3) = previous_position - floor(previous_position/3).

We can see that the position of the number can be tracked using this formula: previous_position - floor(previous_position/step).

Also if the current position becomes equal to the step the number will be removed from the series. And the number becomes a non-lucky number.

Using the above facts let's create a program to find if a number is lucky or not:


# define a function to check if the number is lucky
def isLucky(num):
    # use global step value
    global step
    position = num
    # if the position of the number falls at the step
    # then it will be removed from series
    if position % step == 0:
        return False
    # if step becomes greater than position
    # then it's definitely a lucky number
    if step > position:
        return True
      
    position = position - position / step
    return isLucky(position)


# take input from user
num = int(input("Enter number: "))

# define a global step value
step = 2

lucky = isLucky(num)
if lucky:
    print(num, "is a lucky number.")
else:
    print(num, "is not a lucky number.")

Output:

Enter number: 9
9 is a lucky number.
Enter number: 10
10 is not a lucky number.

Conclusion

In this article, we have learned about the Lucky number, and its program in Python with the complete explanation, and also we have learned about the method to check if a number is lucky or not.

Jump to the next problems Factorial program using python, Fibonacci series using python, etc.