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:
- Using For Loop (Static Input)
- Using For loop (User Input)
- Using Mathematical Formula (Static Input)
- Using Mathematical Formula (User Input)
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.
- Python Program to Print the Natural Numbers Summation Pattern
- Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N
- Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n
- Python Program to Print nth Iteration of Lucas Sequence
- Python Program for Leonardo Number Series