Signup/Sign In

Python Program to Calculate the Sum of Squares of First n Natural Number

In this tutorial, you will learn how to create a program to calculate the sum of squares of first N natural numbers. To execute this program, we will be using the concept of loops in Python. You must know this concept before moving further.

For a positive integer N, the task is to find the value of,

1² + 2² + 3² + 4².....+ N²

The program should accept the following input, look at the sample input and output below,

Input- enter n: 13

Output- sum of square of first 13 natural numbers: 819

For executing this task, we can follow two approaches:

  1. Using loops
  2. Using formula

Approach 1: Using loops

In this approach, we will use the concept of loops to find the squares of the first n numbers and we will keep on adding the numbers to get the final sum.

Algorithm

Step 1- Define a function to find the sum of squares

Step 2- Declare a variable that will store the sum

Step 3- Define a loop that will run n times

Step 4- Inside the loop update the value of the variable which will store the sum of squares

Step 5- Calculate the square of each number before adding it to the sum

Step 6- Return the value of s

Step 7- Take input of n from the user

Step 8- Pass the input as a parameter in the function

Step 9- Display the result returned by the function

Python Program 1

Look at the complete program given below to understand the implementation of the approach.

def SumofSquares(n):
    s=0
    for i in range(n+1):
        s+=i**2
    return s

#input
n=int(input("enter n: "))
print("sum of squares of first {} natural numbers: ".format(n),SumofSquares(n))


enter n: 20
sum of squares of first 20 natural numbers: 2870

The operator ** is used to calculate exponents, i**2 is the same as i²

The format() is a function for handling strings that permits you to do variable substitutions and data formatting. Here, we have used this function to print the value of n in place of {} in the print statement.

Approach 2: Using Formula

In mathematics, there is a formula to calculate the sum of squares of first n numbers. We can directly put this formula to calculate our result. This will eliminate the need for a loop in our program.

sum of squares of first n natural numbers = (n*(n+1)*(2n+1))/6

Algorithm

Step 1- Define a function to calculate the sum of squares

Step 2- Use the formula mentioned above to calculate the sum of squares of n natural numbers

Step 3- Return the value calculated above

Step 4- Take input of n from the user

Step 5- Call the function to display the result

Python Program 2

Look at the complete program given below to understand the implementation of the approach.

def SquareSum(n) :
    return (n * (n + 1) * (2 * n + 1)) // 6
  
n=int(input("enter N: "))
print("Sum of squares of first {} natural numbers: ".format(n),SquareSum(n))


enter N: 10
Sum of squares of first 10 natural numbers: 385

The format() function is used in the same way as mentioned above to display the value of n in place of {} in the print statement.

Conclusion

In this tutorial, we have learned two ways by which we can calculate the sum of the squares of the first N natural numbers. One is by using a loop that will calculate the squares of N numbers and add them up to give the final result. Second, we can directly use the formula to get the value of the sum of the squares of N natural numbers.