Python Program to Find the Sum of Series 3+7+13+21…..+N

In the previous article, we have discussed Python Program to Find the Sum of Series 9+99+999…..+N

Given a number N and the task is to find the sum of the given series (3+7+13+21…..+N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 9

Output:

The total sum of the series till the given number { 9 } =  339

Example2:

Input:

Given Number = 15

Output:

The total sum of the series till the given number { 15 } =  1375

Program to Find the Sum of Series 3+7+13+21…..+N in Python

Below are the ways to find the sum of the given series (3+7+13+21…..+N)  for the given number in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 2 to the given number+1 using the for loop.
  • Inside the loop, subtract 1 from the iterator value and multiply it with the iterator and add 1 to this result.
  • Store it in another variable say p.
  • Add the value of above p to the above-initialized resltsum and store it in the same variable resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 9
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 2 to the given number+1 using the for loop.
for itr in range(2, gvn_numb+2):
  # Inside the loop, subtract 1 from the iterator value and multiply it with the
  # iterator and add 1 to this result.
  # Store it in another variable say p.
    p = 1+(itr*(itr-1))
  # Add the value of above p to the above-initialized resltsum and store it in the same
  # variable resltsum.
    resltsum += p
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 9 } =  339

Method #2: Using For loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 2 to the given number+1 using the for loop.
  • Inside the loop, subtract 1 from the iterator value and multiply it with the iterator and add 1 to this result.
  • Store it in another variable say p.
  • Add the value of above p to the above-initialized resltsum and store it in the same variable resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N 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 say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Loop from 2 to the given number+1 using the for loop.
for itr in range(2, gvn_numb+2):
  # Inside the loop, subtract 1 from the iterator value and multiply it with the
  # iterator and add 1 to this result.
  # Store it in another variable say p.
    p = 1+(itr*(itr-1))
  # Add the value of above p to the above-initialized resltsum and store it in the same
  # variable resltsum.
    resltsum += p
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 15
The total sum of the series till the given number { 15 } = 1375

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.