Babbage problem: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
m →‎Haskell - Safe.headMay: Changed a name, to avoid shadowing Prelude.ceiling
Hout (talk | contribs)
→‎{{header|Haskell}}: Adding a suffix, and testing for an integer root
Line 771:
{{Out}}
<pre>25264 ^ 2 -> 638269696</pre>
 
====Suffixes and integer roots====
 
The inverse approach, which gets us to the first number in just 638 tests, is to append a "269696" suffix to each integer, and test the result for an integer root.
 
We can then take as many matches as we need from an infinite stream of babbages.
 
<lang haskell>import Data.List (intercalate)
 
babbages :: [Integer]
babbages =
filter hasIntRoot ((\x -> read $ show x ++ "269696" :: Integer) <$> [1 ..])
 
hasIntRoot :: Integer -> Bool
hasIntRoot = (==) =<< (^ 2) . floor . sqrt . fromIntegral
 
main :: IO ()
main =
(putStrLn . unlines)
(fmap
(intercalate " -> " .
fmap show . ([floor . sqrt . fromInteger, id] <*>) . pure)
(take 5 babbages))</lang>
{{Out}}
<pre>25264 -> 638269696
99736 -> 9947269696
150264 -> 22579269696
224736 -> 50506269696
275264 -> 75770269696</pre>
 
=={{header|J}}==