2010年2月19日金曜日

problem 6 in python


Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


自分の解答

sum1 = 0
sum2 = 0
for x in range(1,101):
sum1 += x*x
for x in range(1,101):
sum2 += x
sum3 = sum2 * sum2
print sum1,sum3
print sum3 -sum1


コードがひどすぎる。もっと上手にかきたいんだけど、思いつかない。付け焼刃みたい。

模範解答

total1=0
total2=0
for i in range(1, 101):
total1 =total1+(i**2)
total2 = total2+i
print (total2**2)-total1

0 件のコメント:

コメントを投稿