{-
Moritz Venn (MatrNr.: 275840)
Jens Diewald (MatrNr.: 273715)
-}
-- creates never ending list of primes from never ending list of n > 1
primes :: [Int]
primes = primes' [2..]
-- filters multiples of x from never ending list
primes' :: [Int] -> [Int]
primes' (x:xs) = x : primes' (filter (\v -> keinVielfaches x v) xs)
-- y is no multiple of x if y mod x is not 0
keinVielfaches :: Int -> Int -> Bool
keinVielfaches x y | (y `mod` x == 0) = False
| otherwise = True
-- checks if int is prime
isPrim :: Int -> Bool
isPrim x = isPrim' x primes
-- walks through the list until int == prime or int < prime
isPrim' :: Int -> [Int] -> Bool
isPrim' y (x:xs)
| (y==x) = True
| (y> x) = isPrim' y xs
| otherwise = False
-- returns nth (first element is 1) prime
prime :: Int -> Int
-- only accept possitive numbers
prime (x+1) = prime' x primes
{-
returns n-th element (first element is 0) of a list (which needs to have
at least that much elements)
-}
prime' :: Int -> [Int] -> Int
prime' 0 (x:xs) = x
prime' (n+1) (x:xs) = prime' n xs
-- returns primes up to (and including) x
takePrimes :: Int -> [Int]
takePrimes x = takePrimes' x primes
-- appends elements from list until element is smaller than y
takePrimes' :: Int -> [Int] -> [Int]
takePrimes' y (x:xs)
| (x<=y) = x: takePrimes' y xs
| otherwise = []