primes = sieve $ 2:[3,5..]
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
solve :: Int
solve = foldl (\acc x -> acc * calcMiniMax20 x 1) 1 $ takeWhile (<20) primes
calcMiniMax20 x n
| n * x > 20 = n
| otherwise = calcMiniMax20 x (x*n)
solve :: Int
solve = foldl (\acc x -> acc * calcMiniMax20 x 1) 1 $ takeWhile (<20) primes
calcMiniMax20 :: Int -> Int -> Int
calcMiniMax20 x n
| n * x > 20 = n
| otherwise = calcMiniMax20 x (x*n)
primes :: [Int]
primes = sieve $ 2:[3,5..]
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
print solve
powerCons :: [Int] -> [Int]
powerCons [] = []
powerCons (x:xs)
| x == 1 = powerCons xs
| otherwise = let (v, s) = runState (reduction x 1) xs in x^v : (powerCons s)
reduction :: Int -> Int -> State [Int] Int
reduction x n = do
list <- gets $ map (\y -> if isDivisible x y then y `div` x else y)
put list
if any (isDivisible x) list
then do
reduction x (n+1)
else return n
-- 割り切れるかどうか
isDivisible :: Int -> (Int -> Bool)
isDivisible a = \b -> b `mod` a == 0
import Control.Monad.State
powerCons :: [Int] -> [Int]
powerCons [] = []
powerCons (x:xs)
| x == 1 = powerCons xs
| otherwise = let (v, s) = runState (reduction x 1) xs in x^v : powerCons s
reduction :: Int -> Int -> State [Int] Int
reduction x n = do
list <- gets $ map (\y -> if isDivisible x y then y `div` x else y)
put list
if any (isDivisible x) list
then do
reduction x (n+1)
else return n
isDivisible :: Int -> Int -> Bool
isDivisible a b = b `mod` a == 0
print $ product $ powerCons [1..20]