haskell: add [p4, p10]

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-10-27 08:54:11 +05:30
parent e3f9ab49f3
commit ad0acc15c9
8 changed files with 81 additions and 2 deletions

View File

@@ -1,13 +1,19 @@
module Lib (fib, primeFactors) where
module Lib (fib, primes, primeFactors) where
fib :: [Integer]
fib = 1 : 2 : zipWith (+) fib (tail fib)
isPrime :: Integer -> Bool
isPrime n = n > 1 && null [[] | k <- [2 .. (floor $ sqrt $ fromIntegral n)], mod n k == 0]
primes :: [Integer]
primes = 2 : filter isPrime [3, 4 ..]
primeFactors :: Integer -> [Integer]
primeFactors n = genPrimeFactors n 2
where
genPrimeFactors :: Integer -> Integer -> [Integer]
genPrimeFactors n c
| c == n = [n]
| c * c > n = [n]
| mod n c /= 0 = genPrimeFactors n (c + 1)
| otherwise = c : genPrimeFactors (div n c) c