[Python] 6 – Sum square difference

[문제 링크]
http://projecteuler.net/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.

[문제 설명]
1~100까지 합의 제곱 에서 1의 제곱 더하기 2의 제곱 더하기 … 100의 제곱을 더한값을 뺀 값을 구하라.

[소스 코드]

import time
sTime = time.clock()
sqrAndSum = 0
sumAndSqr = 0
for i in range(1, 101):
    sqrAndSum = sqrAndSum + (i*i)

sumAndSqr = 0
for i in range(1, 101):
    sumAndSqr = sumAndSqr + i
sumAndSqr = (sumAndSqr * sumAndSqr)

print("Result=", sumAndSqr - sqrAndSum, "time=", (time.clock() - sTime) * 100)

[출력 결과]

(‘Result=’, 25164150, ‘time=’, 0.007399999999999768)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s