1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Project Euler】Problem 6: 2乗の和の差

Posted at
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて回答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします。

問題 6. 2乗の和の差

原文 Problem 6: Sum square difference

問題の要約:1から100までの2乗の和と和の2乗と差を求めよ

比較的簡単な問題なので、pythonのmapとlambdaを使って短く書くと以下のようになります。

nl = list(range(1,100+1))
print(f"Anwer : {sum(nl)**2-sum(map(lambda x: x**2, nl))}") 

またちょっと凝って以下の公式を使ってみると。1行で書くことも。

(a+b+c)^2 - (a^2+b^2+c^2) \\
= 2ab+2bc+2ca 
import itertools
print(f"Anwer : {sum([2*a*b for a,b in itertools.combinations(range(1,100+1),2) ])}")
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?