haskell: add p3

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-10-27 04:21:00 +05:30
parent 4e98bf2115
commit e3f9ab49f3
2 changed files with 18 additions and 2 deletions

View File

@@ -1,4 +1,13 @@
module Lib where module Lib (fib, primeFactors) where
fib :: [ Integer ] fib :: [Integer]
fib = 1 : 2 : zipWith (+) fib (tail fib) fib = 1 : 2 : zipWith (+) fib (tail fib)
primeFactors :: Integer -> [Integer]
primeFactors n = genPrimeFactors n 2
where
genPrimeFactors :: Integer -> Integer -> [Integer]
genPrimeFactors n c
| c == n = [n]
| mod n c /= 0 = genPrimeFactors n (c + 1)
| otherwise = c : genPrimeFactors (div n c) c

7
haskell/p3.hs Normal file
View File

@@ -0,0 +1,7 @@
import Lib (primeFactors)
main :: IO ()
main = putStr $ show solve
solve :: Integer
solve = last $ primeFactors 600851475143