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

Sum square difference - Project Euler Solution

codeperfectplus profile image CodePerfectPlus Updated on ・1 min read

Problem Solving Series(For Interview) (9 Part Series)

1) Multiples of 3 and 5 - Project Euler Solution 2) Two sum - Leet Code Solution 3 ... 7 3) Check for Armstrong Number 4) Largest palindrome product - Project Euler Solution 5) Smallest multiple - Project Euler Solution 6) Sum square difference - Project Euler Solution 7) 10001st prime - Project Euler Soution 8) Sieve of Eratosthenes 9) Bubble Sort Implementation in Python

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

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

(1+2+3...+10)^2 = 55^2 = 3025

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

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))

Share Your Solutions for Sum square difference.

Problem Solving Series(For Interview) (9 Part Series)

1) Multiples of 3 and 5 - Project Euler Solution 2) Two sum - Leet Code Solution 3 ... 7 3) Check for Armstrong Number 4) Largest palindrome product - Project Euler Solution 5) Smallest multiple - Project Euler Solution 6) Sum square difference - Project Euler Solution 7) 10001st prime - Project Euler Soution 8) Sieve of Eratosthenes 9) Bubble Sort Implementation in Python

Posted on by:

codeperfectplus profile

CodePerfectPlus

@codeperfectplus

Skilled in Data Science, Machine Learning, Deep Learning, As Well As Web Development Knowledge. React ❤️ to encourage Author.

Discussion

markdown guide