Python Program to Print Series 1, 3, 7, 15, 31 … N

1 3 7 15 31: In the previous article, we have discussed Python Program to Print Series 2, 4, 7, 2, 21, … N
Given a number N and the task is to print the series (1, 3, 7, 15, 31 … N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 5

Output:

The above series till the given number{ 5 } is :
1 3 7 15 31

Example2:

Input:

Given Number (Limit) = 9

Output:

The above series till the given number{ 9 } is :
1 3 7 15 31 63 127 255 511

Program to Print Series 1, 3, 7, 15, 31 … N in Python

Below are the ways to print the series (1, 3, 7, 15, 31 … N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N (Limit) as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say previous_val and initialize its value to 0.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and add 1 to the result.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 5
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say previous_val and initialize its value to 0.
previous_val = 0
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while itr <= gvn_numb:
    # Inside the loop, Multiply the variable previous_val with 2 and add 1 to the result.
        # Store it in the same variable previous_val.
    previous_val = (previous_val * 2) + 1
    # Print the value of the above previous_val separated by spaces.
    print(previous_val, end=" ")
    # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 5 } is :
1 3 7 15 31

Method #2: Using While loop (User Input)

Approach:

  • Give the number N (Limit) as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say previous_val and initialize its value to 0.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and add 1 to the result.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (Limit) as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say previous_val and initialize its value to 0.
previous_val = 0
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while itr <= gvn_numb:
    # Inside the loop, Multiply the variable previous_val with 2 and add 1 to the result.
        # Store it in the same variable previous_val.
    previous_val = (previous_val * 2) + 1
    # Print the value of the above previous_val separated by spaces.
    print(previous_val, end=" ")
    # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 9
The above series till the given number{ 9 } is :
1 3 7 15 31 63 127 255 511

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.