Python Math: Calculate the difference between the squared sum and the sum of squared of first n natural numbers
11. Difference of Squares Sum and Sum of Squares
Write a Python program to calculate the difference between the squared sum of the first n natural numbers and the sum of squared first n natural numbers.(default value of number=2).
Sample Solution:
Python Code:
def sum_difference(n=2):
sum_of_squares = 0
square_of_sum = 0
for num in range(1, n+1):
sum_of_squares += num * num
square_of_sum += num
square_of_sum = square_of_sum ** 2
return square_of_sum - sum_of_squares
print(sum_difference(12))
Sample Output:
5434
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to calculate the difference between the square of the sum and the sum of the squares of the first n natural numbers, and print the result.
- Write a Python function that computes both the square of the sum and the sum of the squares for n numbers, then returns their difference.
- Write a Python script to verify the difference calculation for a range of n values and output the result in a formatted table.
- Write a Python program to compare the computed difference for n=12 with that of n=10 and n=15, and then print the comparisons.
Go to:
Previous: Write a Python program to find the smallest multiple of the first n numbers. Also, display the factors.
Next: Write a Python program to calculate the sum of all digits of the base to the specified power.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.