Python Project: Sum of the Square and Square of the Sum
Python: Sum of the Square and Square of the Sum
Difference of sum of the square and the square of the sum
I fond this on projecteuler.net Usually I add some steps to there problems to make it more application look and feel. Later on, we’ll see how.
Problem assumption: If we said that we have a range of numbers (1,10) then the sum of this range is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55, Now the Square of the sum is 55^2; thats mean the Square of the sum of (55) = 3025.
And for the same range, the Sum of Square means that we will get the Square of each number in the range then will get there summation. So with our range (1,10) Sum of Square is 1^2 + 2^2 + 3^2 + 4^2 + 5^2 …. + 10^2 = 385
The Problem #6 in ProjectEuler
The sum of the squares of the first ten natural numbers is,12 + 22 + … + 102= 385
The square of the sum of the first ten natural numbers is,(1 + 2 + … + 10)2 = 552= 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.
The Task:Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Inhancment Now to make this task workking as application and to get more general output of it, first we will ask the user to input a range of the numbers, then we will applay the function on that range.
The Code:
# Problem 6 in ProjectEuler.
def square_of_sum(num1, num2):
tot = 0
for x in range(num1, num2+1):
tot = tot+x
print(‘The Square of the Sun’,tot*tot)
return tot*tot
def sum_of_square(num1, num2):
tot = 0
for x in range(num1, num2+1):
tot = tot+(x*x)
print(‘The Sum of the Square ‘, tot)
return tot
#Ask the user for his input.
num1 = int(input(‘Enter First number in the range: ‘))
num2 = int(input(‘Enter the last number in the range: ‘))
#Call the functions.
w1 = square_of_sum(num1, num2)
w2 = sum_of_square(num1, num2)
#Output the Difference.
print(‘The Difference is: ‘, w1-w2)