When you don't sew a little hole, you'll have to sew a big one
Such a worldly wisdom perfectly match both software and socks.
Se non cuci un buchino, dovrai cucire un bucone.
Saggezza popolare perfettamente applicabile al software come ai calzini.
When you don't sew a little hole, you'll have to sew a big one
Se non cuci un buchino, dovrai cucire un bucone.
import Data.List
solution = head $ reverse $ factors 600851475143
factors :: Integral a => a -> [a]
factors x = unfoldr nextFactor (x, primes)
where nextFactor (1, _) = Nothing
nextFactor (x, p:ps)
| mod x p == 0 = Just (p, (divAll x p, ps))
| otherwise = nextFactor (x, ps)
divAll x factor
| mod x factor == 0 = divAll (div x factor) factor
| otherwise = x
primes :: Integral a => [a]
primes = 2 : iterate nextPrime 3
where nextPrime x = head $ filter isPrime[(x+1)..]
isPrime :: Integral a => a -> Bool
isPrime x = and $ [mod x p /= 0 | p <- takeWhile lowerThanSqrtX primes]
where lowerThanSqrtX candidate = candidate <= (round $ sqrt $ fromIntegral x)
solution = sum $ filter even $ fibsLowerThan 4000000
fibsLowerThan upperBound = fibs 0 upperBound
fibs :: Integral a ⇒ a → a → [a]
fibs n upperBound
| fib n > upperBound = []
| otherwise = fib n : fibs (n + 1) upperBound
fib 0 = 0
fib 1 = 1
fib n = sum [fib $ n - 1, fib $ n - 2]
solution = sum $ [x | x ← [1..999], or [mod x 3 ≡ 0, mod x 5 ≡ 0]]