/dev/Chiheb-Nexus

Solution de Projecteuler.net: Problème 6 (Python)

# Project Euler: Problem 6
#
# The sum of the squares of the first ten natural numbers is,
#
# 1^2 + 2^2 + ... + 10^2 = 385
# The square of the sum of the first ten natural numbers is,
#
# (1 + 2 + ... + 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.
#
####################################
# Solution: 25164150 
# Best time: 5.2928924560546875e-05
####################################

from time import time

start = time()
limit = 100
f_squares = sum(k**2 for k in range(limit + 1))
n_quares = sum(k for k in range(limit + 1 )) **2
diff = n_quares - f_squares
elapsed = time() - start
print("Solution: {0} \t Time elapsed: {1}".format(diff, elapsed))