Euler's sum of powers conjecture: Difference between revisions
m
→{{header|Haskell}}: Tidied.
m (→{{header|Haskell}}: Tidied.) |
m (→{{header|Haskell}}: Tidied.) |
||
Line 1,726:
import Data.Maybe (maybe)
------------- EULER'S SUM OF POWERS CONJECTURE -----------
xs = [1 .. 249]▼
counterExample sumMap powerMap =
powerMap = M.fromList (zip ((^ 5) <$> xs) xs)▼
find▼
(\(p, s) -> M.member (p - s) sumMap)▼
(M.keys powerMap >>=▼
(\p -> takeWhile (< p) (M.keys sumMap) >>= (\s -> [(p, s)])))▼
sumMapForRange xs =
M.fromList
[ ((x ^ 5) + (y ^ 5), (x, y))
Line 1,740 ⟶ 1,743:
, x > y ]
▲ find
--------------------------- TEST -------------------------
▲ (\(p, s) -> M.member (p - s) sumMap)
main :: IO ()▼
▲ (M.keys powerMap >>=
main = do▼
▲ (\p -> takeWhile (< p) (M.keys sumMap) >>= (\s -> [(p, s)])))
▲ let xs = [1 .. 249]
sumMap = sumMapForRange xs
powerMap = powerMapForRange xs
putStrLn $▼
"Euler's sum of powers conjecture – " <>▼
maybe▼
("no counter-example found in the range " <> rangeString xs)▼
(showExample sumMap powerMap xs)
(counterExample sumMap powerMap)
showExample :: M.Map Int (Int, Int) -> M.Map Int Int -> [Int] -> (Int, Int) -> String
showExample sumMap powerMap xs (p, s) =
"a counter-example in range " <> rangeString xs <> ":\n\n" <>
intercalate "^5 + " (show <$> [a, b, c, d]) <>
Line 1,760 ⟶ 1,772:
rangeString :: [Int] -> String
rangeString [] = "[]"
rangeString (x:xs) = '[' : show x <> " .. " <> show (last xs) <> "]"</lang>
▲main :: IO ()
▲main =
▲ putStrLn $
▲ "Euler's sum of powers conjecture – " <>
▲ maybe
▲ ("no counter-example found in the range " <> rangeString xs)
{{Out}}
<pre>Euler's sum of powers conjecture – a counter-example in range [1 .. 249]:
|