DEV Community

loading...
Cover image for Sum square difference - Project Euler Solution

Sum square difference - Project Euler Solution

Deepak Raj
Skilled in Data Science, Machine Learning, Deep Learning, As Well As Web Development Knowledge. React ❤️ to encourage Author.
Updated on ・1 min read

Sum square difference - Project Euler Solution

Topic: Sum square difference

Problem Statement:

The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + ... + 10^2 = 385
Enter fullscreen mode Exit fullscreen mode

The square of the sum of the first ten natural numbers is,

(1+2+3...+10)^2 = 55^2 = 3025
Enter fullscreen mode Exit fullscreen mode

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .

3025 - 385 = 2640
Enter fullscreen mode Exit fullscreen mode

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

You can find the original question here -> Project Euler

Sum square difference - Project Euler Solution in python

def difference(n):
    """
        This program will return difference between the square of sum and sum of the square. 
    """
    sumOfSquare = 0
    squareOfSum = 0
    for i in range(n+1):
        squareOfSum += i  
        sumOfSquare += i ** 2
    return squareOfSum ** 2 - sumOfSquare

if __name__ == "__main__":
    print(difference(100))
Enter fullscreen mode Exit fullscreen mode

Share Your Solutions for Sum square difference.

Discussion (0)

Forem Open with the Forem app