Program to find the Sum of Series 1^2+2^2+3^2...+N^2

Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2

In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop
Given a number N the task is to find the sum of the given series 1^2+2^2+3^2…+N^2 for the given number in Python.

Examples:

Example1:

Input:

Given Number =3

Output:

The sum of the series till the given number { 3 } is : 14

Example2:

Input:

Given Number =5

Output:

The sum of the series till the given number { 6 } is : 55

Program to find the Sum of Series 1^2+2^2+3^2…+N^2 in Python

Below are the ways to find the sum of the given series 1^2+2^2+3^2…+N^2 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 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value * iterator value and store it in a variable.
  • Add the above variable to the 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.
gvnNumbr = 5
# 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 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop,
    # Calculate the value of iterator value * iterator value and store it in a variable.
    squarevalu = k*k
    # Add the above variable to the resltsum
    resltsum = resltsum+squarevalu

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 5 } is : 55

Time Complexity: O(N) where N is the value of the given number (As the loop runs N times)

Method #2: Using For loop and pow() function (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 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value * iterator value and store it in a variable.
  • Add the above variable to the 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.
gvnNumbr = int(input('Enter some random number N = '))
# 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 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop,
    # Calculate the value of iterator value * iterator value and store it in a variable.
    squarevalu = k*k
    # Add the above variable to the resltsum
    resltsum = resltsum+squarevalu

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 3
The sum of the series till the given number { 3 } is : 14

Time Complexity: O(N) where N is the value of the given number (As the loop runs N times)

Method #3: Using Mathematical Formula (Static Input)

We can calculate the sum of the above series directly by using the mathematical formula :

(n * ( n + 1 ) *( 2 * n +1) ) / 6

Approach:

  • Give the number N as static input and store it in a variable.
  • Calculate the sum of the given series using the above mathematical formula and store it in a variable to say 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.
gvnNumbr = 5
# Calculate the sum of the given series using the above mathematical formula
# and store it in a variable to say resltsum
resltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))//6

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 5 } is : 55

Time Complexity: O(1)

Method #4: Using For loop and ** operator (User Input)

We can calculate the sum of the above series directly by using the mathematical formula :

(n * ( n + 1 ) *( 2 * n +1) ) / 6

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Calculate the sum of the given series using the above mathematical formula and store it in a variable to say 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.
gvnNumbr = int(input('Enter some random number N = '))
# Calculate the sum of the given series using the above mathematical formula
# and store it in a variable to say resltsum
resltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))//6

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 8
The sum of the series till the given number { 8 } is : 204

Time Complexity: O(1)

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.