Problem 5 「最小の倍数」 †
2520 は 1 から 10 の数字の全ての整数で割り切れる数字であり, そのような数字の中では最小の値である.
では, 1 から 20 までの整数全てで割り切れる数字の中で最小の正の数はいくらになる
haskell解 20入力で答えが出る。
import Data.List
import qualified Data.Map as M
import System.IO
main=do
n <- getLine
putStrLn $ show . g $ read n
g :: Int->Int
g n=product $ map h $ M.toList $ M.fromListWith max (flatten [map (\e ->(head e,length e) )
(group (f 2 x)) | x<-[2..n]])
h::(Int,Int)->Int
h (a,b)=a^b
f :: Int->Int->[Int]
f _ 1=[]
f d n=if n `mod` d==0
then d : f d (n `div` d)
else f (d+1) n
flatten :: [[a]] -> [a]
flatten xs = (\z n -> foldr (\x y -> foldr z y x) n xs) (:) []
Prolog解
X is 2^4*3^2*5*7*11*13*17*19.
最終更新:2018年04月18日 09:24