type Primes = [Integer]
solve :: Integer -> Primes -> Integer
solve n primes@(p:ps)
| n < p^2 = n
| n `mod` p == 0 = solve (n `div` p) primes
| otherwise = solve n ps
primes = sieve $ 2:[3,5..] -- 偶数は抜くというせめてものあがき
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
type Primes = [Integer]
solve :: Integer -> Primes -> Integer
solve n primes@(p:ps)
| n < p^2 = n
| n `mod` p == 0 = solve (n `div` p) primes
| otherwise = solve n ps
primes :: Primes
primes = sieve $ 2:[3,5..]
sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
print $ solve 600851475143 primes