Skip to content

Instantly share code, notes, and snippets.

@9re
Last active February 5, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9re/5721688 to your computer and use it in GitHub Desktop.
Save 9re/5721688 to your computer and use it in GitHub Desktop.
list up rational numbers p such that sqrt(p), sqrt (p - 5), sqrt (p + 5) are all rational.

list up rational numbers p such that sqrt(p), sqrt (p - 5), sqrt (p + 5) are all rational.

*Main> take 2 answer
[1681 % 144,11183412793921 % 2234116132416]
answer =
map
conversion
(filter
(\(x, y, z) -> test x y)
pythagorean_triples)
conversion :: (Integer, Integer, Integer) -> Rational
conversion (x, y, z) =
toRational (z * z) / (toRational (4 * x * y) / toRational 10)
test :: Integer -> Integer -> Bool
test x y =
(w `mod` 10) == 0 &&
is_squre (
floor ((fromInteger w) / 10))
where
w = x * y
pythagorean_triples =
filter
(\(x, y, z) -> gcd x y == 1)
[(m * m - n * n, 2 * m * n, m * m + n * n) | m <- [1..], n <- [2..m-1]]
is_squre :: Integer -> Bool
is_squre a =
a == (floor . sqrt . fromInteger) a ^ 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment