LoginSignup
5
5

More than 5 years have passed since last update.

Haskellで直角三角形のタプルをつくる

Posted at

まずは、1〜100の3辺x, y ,zの組み合わせをつくる。

[(x, y, z) | x <- [1..100], y <- [1..100], z <- [1..100]]

ここで、zを斜辺だとすると、xとyはzより小さいことになる。その組み合わせをつくると、

[(x, y, z) | z <- [1..100], y <- [1..z], x <- [1..z]]

x,y,zの順番を少し入れ替えていることに注意。
xとyは、yの方を長辺だとすると、さらにこう書ける。

[(x, y, z) | z <- [1..100], y <- [1..z], x <- [1..y]]

あとは、x^2 + y^2 = z^2を満たす組み合わせを抽出する。

[(x, y, z) | z <- [1..100], y <- [1..z], x <- [1..y], (x * x + y * y == z * z)]

以上で完成。

5
5
0

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
5
5