Python – Program for Sum of squares of first n natural numbers

Python – Program for Sum of squares of first n natural numbers

In this post, We will see how to write a python program for finding Sum of squares of first n natural numbers in different ways.

Example:

Input: N = 2
Output: 1 * 1 + 2 * 2 => 5 OR 
12 + 2 2 => 5

Input: N = 3
Output: 1 * 1 + 2 * 2 + 3 * 3 => 14 OR
12 + 2 2 + 3 2 => 14

Now, Let’s see the codes:

Code 1: Run a loop from 2 to N and for each i, find i 2 and adding to the sum.

# first 5 natural numbers
N = 5

# initialize variable with 1
sumSquare = 1

# loop from 2 to N
for i in range(2, N + 1) :
    
    # square summation
    sumSquare += i ** 2
    
# print the result
print("Sum of the Squares of 1st", N ,
      "Natural Numbers is:",sumSquare)

Output:

Sum of the Squares of 1st 5 Natural Numbers is: 55

Code 2: Create user defined function, main and then apply the previous code logic.

# Define a user defined 
# function for finding
# sum of squares of 1st
# N natural Numbers
def sumOfSquares(Num) :

    # initialize variable with 1
    sumSquare = 1

    # loop from 2 to N
    for i in range(2, Num + 1) :

        # square summation
        sumSquare += i ** 2
        
    # return result
    return sumSquare

# main code
if __name__ == "__main__" :

    # first 5 natural numbers
    Num = 5
    
    # function calling and
    # printing the result
    print("Sum of the Squares of first", Num ,
          "Natural Numbers is:",
          sumOfSquares(Num))

Output:

Sum of the Squares of first 5 Natural Numbers is: 55

Code 3: Run a loop from 2 to N and for each i, find i * i and adding to the sum.

# Define a user defined 
# function for finding
# sum of squares of 1st
# N natural Numbers
def sumOfSquares(Num) :

    # initialize variable with 1
    sumSquare = 1

    # loop from 2 to N
    for i in range(2, Num + 1) :

        # square summation
        sumSquare += i * i
        
    # return result
    return sumSquare

# main code
if __name__ == "__main__" :

    # first 5 natural numbers
    Num = 5
    
    # function calling and
    # printing the result
    print("Sum of the Squares of 1st", Num ,
          "Natural Numbers is:",
          sumOfSquares(Num))

Output:

Sum of the Squares of 1st 5 Natural Numbers is: 55

Code 4: By Using given formula :

sum of the squares of 1st N Natural numbers:
 ((N) * (N + 1) * (2 * N + 1)) / 6
# Define a user defined 
# function for finding
# sum of squares of 1st
# N natural Numbers
def sumOfSquares(Num) :
    
    # return result
    return ((Num) * (Num + 1) * (2 * Num + 1)) / 6
    
# main code
if __name__ == "__main__" :

    # first 5 natural numbers
    Num = 5
    
    # function calling and
    # printing the result
    print("Sum of the Squares of 1st", Num ,
          "Natural Numbers is:",
          sumOfSquares(Num))

Output:

Sum of Squares of first 5 Natural Numbers is: 55

Code 5: By making a anonymous function.

# create a annonymous
# function for returning
# sum of squares of 1st
# N natural Numbers
sumOfSquares = lambda Num : ((Num) * (Num + 1) * (2 * Num + 1)) / 6
    
# main code
if __name__ == "__main__" :

    # first 5 natural numbers
    Num = 5
    
    # function calling and
    # printing the result
    print("Sum of the Squares of 1st", Num ,
          "Natural Numbers is:",
          sumOfSquares(Num))

Output:

Sum of the Squares of 1st 5 Natural Numbers is: 55

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds