-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimeutils.hs
More file actions
31 lines (23 loc) · 1.01 KB
/
primeutils.hs
File metadata and controls
31 lines (23 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Primeutils where
import Shared(mergeLists,listDivisors)
primeFactors x
| x < 2 = error "You must not use a number lower than two as an argument."
| otherwise = filter (isPrime) (listDivisors x)
isPrime x
| x < 0 = error "You must not use a number lower than zero as an argument."
| x == 1 = False
| (listDivisors x) !! 1 == x = True
| otherwise = False
primesBetween x y = [n | n <- [x..y], isPrime n == True]
firstPrimeFactor x = (primeFactors x) !! 0
primes = [n | n <- [2,3..], isPrime n == True]
filterPrimesBy (x) = [n | n <- [2,3..], isPrime n == True, x n == True]
-- | lists all primes from 2 to the first parameter, using Eratosthenes' method
eratosthenes :: Integer -> [Integer]
eratosthenes x = do
let pool = 2:[3,5..x]
let multipleFilter [] = []
let multipleFilter (x:xs)
| length xs == 1 = x:(head xs):[]
| otherwise = x : multipleFilter (filter (\y -> (y `mod` x /= 0)) xs)
multipleFilter pool