Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) 2640 >>> solution(15) 13160 >>> solution(20) 41230 >>> solution(50) 1582700
(n: int = 100)
| 18 | |
| 19 | |
| 20 | def solution(n: int = 100) -> int: |
| 21 | """ |
| 22 | Returns the difference between the sum of the squares of the first n |
| 23 | natural numbers and the square of the sum. |
| 24 | |
| 25 | >>> solution(10) |
| 26 | 2640 |
| 27 | >>> solution(15) |
| 28 | 13160 |
| 29 | >>> solution(20) |
| 30 | 41230 |
| 31 | >>> solution(50) |
| 32 | 1582700 |
| 33 | """ |
| 34 | |
| 35 | sum_of_squares = 0 |
| 36 | sum_of_ints = 0 |
| 37 | for i in range(1, n + 1): |
| 38 | sum_of_squares += i**2 |
| 39 | sum_of_ints += i |
| 40 | return sum_of_ints**2 - sum_of_squares |
| 41 | |
| 42 | |
| 43 | if __name__ == "__main__": |