Jump to content

Magnanimous numbers: Difference between revisions

no edit summary
(add FreeBASIC)
No edit summary
Line 796:
391st through 400th magnanimous numbers:
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
</pre>
 
=={{header|Haskell}}==
<lang haskell>import Data.List.Split ( chunksOf )
import Data.List ( (!!) )
 
isPrime :: Int -> Bool
isPrime n
|n == 2 = True
|n == 1 = False
|otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root]
where
root :: Int
root = floor $ sqrt $ fromIntegral n
isMagnanimous :: Int -> Bool
isMagnanimous n = all isPrime $ map (\p -> fst p + snd p ) numberPairs
where
str:: String
str = show n
splitStrings :: [(String , String)]
splitStrings = map (\i -> splitAt i str) [1 .. length str - 1]
numberPairs :: [(Int , Int)]
numberPairs = map (\p -> ( read $ fst p , read $ snd p )) splitStrings
 
printInWidth :: Int -> Int -> String
printInWidth number width = replicate ( width - l ) ' ' ++ str
where
str :: String
str = show number
l :: Int
l = length str
 
solution :: [Int]
solution = take 400 $ filter isMagnanimous [0 , 1 ..]
 
main :: IO ( )
main = do
let numbers = solution
numberlines = chunksOf 10 $ take 45 numbers
putStrLn "First 45 magnanimous numbers:"
mapM_ (\li -> putStrLn (foldl1 ( ++ ) $ map (\n -> printInWidth n 6 )
li )) numberlines
putStrLn "241'st to 250th magnanimous numbers:"
putStr $ show ( numbers !! 240 )
putStrLn ( foldl1 ( ++ ) $ map(\n -> printInWidth n 8 ) $ take 9 $
drop 241 numbers )
putStrLn "391'st to 400th magnanimous numbers:"
putStr $ show ( numbers !! 390 )
putStrLn ( foldl1 ( ++ ) $ map(\n -> printInWidth n 8 ) $ drop 391 numbers)</lang>
{{out}}
<pre>First 45 magnanimous numbers:
0 1 2 3 4 5 6 7 8 9
11 12 14 16 20 21 23 25 29 30
32 34 38 41 43 47 49 50 52 56
58 61 65 67 70 74 76 83 85 89
92 94 98 101 110
241'st to 250th magnanimous numbers:
17992 19972 20209 20261 20861 22061 22201 22801 22885 24407
391'st to 400th magnanimous numbers:
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.