Python Math: Calculate the difference between the squared sum and the sum of squared of first n natural numbers
Python Math: Exercise-11 with Solution
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:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/math/python-math-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics