Sum square difference
Problem 6
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.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Toplam kare farkı
Problem 6
İlk 10 doğal sayının kareleri toplamı, 12 + 22 + ... + 102 = 385' dir.
İlk 10 doğal sayının toplamlarının karesi, (1 + 2 + ... + 10)2 = 552 = 3025' dir.
İlk on doğal sayının toplamlarının karesi ile karelerinin toplamının farkı,
3025 − 385 = 2640 ' dir.
3025 − 385 = 2640 ' dir.
İlk 100 doğal sayının toplamlarının karesi ile karelerinin toplamının farkını bulun.
Programming perspective is good but, there are closed form formulas for both equations:
YanıtlaSilsum(1..n)^2 = (n(n+1)/2)^2
sum(1^2..n^2) = n(n+1)(2n+1)/6
You can calculate the difference in fixed number of calculations regardless of n: O(1)