From e3f9ab49f343f894fb1f322d2f51111fa2632546 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Thu, 27 Oct 2022 04:21:00 +0530 Subject: [PATCH] haskell: add p3 Signed-off-by: Amneesh Singh --- haskell/Lib.hs | 13 +++++++++++-- haskell/p3.hs | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 haskell/p3.hs diff --git a/haskell/Lib.hs b/haskell/Lib.hs index e932c33..ceab2bc 100644 --- a/haskell/Lib.hs +++ b/haskell/Lib.hs @@ -1,4 +1,13 @@ -module Lib where +module Lib (fib, primeFactors) where -fib :: [ Integer ] +fib :: [Integer] 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 diff --git a/haskell/p3.hs b/haskell/p3.hs new file mode 100644 index 0000000..32c5a56 --- /dev/null +++ b/haskell/p3.hs @@ -0,0 +1,7 @@ +import Lib (primeFactors) + +main :: IO () +main = putStr $ show solve + +solve :: Integer +solve = last $ primeFactors 600851475143