Skip to main content
Code Review

Return to Revisions

2 of 2
Quoted problem properly
200_success
  • 145.6k
  • 22
  • 190
  • 479

Sum of Squares/Square of Sum Difference

I am doing Project Euler problem #6. This is the question it asks:

The sum of the squares of the first ten natural numbers is,

$1ドル^2 + 2^2 + \ldots + 10^2 = 385$$

The square of the sum of the first ten natural numbers is,

$$(1 + 2 + \ldots + 10)^2 = 55^2 = 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.

My implementation in Python is as follows:

def sum_of_squares(n):
 return sum([i**2 for i in range(1, n+1)])
def square_of_sum(n):
 return sum(range(1, n+1)) ** 2
print square_of_sum(100) - sum_of_squares(100)

It works but I was wondering if my implementation is good and fast enough?

user49487
  • 253
  • 1
  • 4
  • 8
lang-py

AltStyle によって変換されたページ (->オリジナル) /