-
Notifications
You must be signed in to change notification settings - Fork 1
/
P25054.hs
88 lines (80 loc) · 4.03 KB
/
P25054.hs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
------------------------------------------------------------------------------------
------------------------------------- MYLENGTH -------------------------------------
------------------------------------------------------------------------------------
myLength :: [Int] -> Int
myLength [] = 0
myLength (_:tail) = 1 + (myLength tail)
------------------------------------------------------------------------------------
------------------------------------- MYMAXIMUM ------------------------------------
------------------------------------------------------------------------------------
myMaximum :: [Int] -> Int
myMaximum [last] = last
myMaximum (head:tail) = max head (myMaximum tail)
------------------------------------------------------------------------------------
------------------------------------- AVERAGE --------------------------------------
------------------------------------------------------------------------------------
totalSum :: [Int] -> Float
totalSum [] = 0.0
totalSum (head:tail) = fromIntegral head + (totalSum tail)
average :: [Int] -> Float
average array = totalSum array / fromIntegral (myLength array)
------------------------------------------------------------------------------------
------------------------------------ PALINDROME ------------------------------------
------------------------------------------------------------------------------------
reverseArray :: [Int] -> [Int]
reverseArray [last] = [last]
reverseArray (head:tail) = (reverseArray tail) ++ [head]
buildPalindrome :: [Int] -> [Int]
buildPalindrome [] = []
buildPalindrome array = (reverseArray array) ++ (array)
------------------------------------------------------------------------------------
-------------------------------------- REMOVE --------------------------------------
------------------------------------------------------------------------------------
remove :: [Int] -> [Int] -> [Int]
remove l1 [] = l1
remove l1 (x:l2) = remove (remove' l1 x) l2
where
remove' :: [Int] -> Int -> [Int]
remove' [] _ = []
remove' (x:l) y
| x == y = remove' l y
| otherwise = x:(remove' l y)
------------------------------------------------------------------------------------
-------------------------------------- FLATTEN -------------------------------------
------------------------------------------------------------------------------------
flatten :: [[Int]] -> [Int]
flatten [] = []
flatten [lastArray] = lastArray
flatten (head:tail) = head ++ flatten tail
------------------------------------------------------------------------------------
------------------------------------ ODDSNEVENS ------------------------------------
------------------------------------------------------------------------------------
oddsNevens :: [Int] -> ([Int], [Int])
oddsNevens [] = ([],[])
oddsNevens array = (oddsNevens' array [] [])
where
oddsNevens' :: [Int] -> [Int] -> [Int] -> ([Int], [Int])
oddsNevens' [] odds evens = (odds, evens)
oddsNevens' (head:tail) odds evens
| even head = (oddsNevens' tail odds (evens ++ [head]))
| otherwise = (oddsNevens' tail (odds ++ [head]) evens)
------------------------------------------------------------------------------------
---------------------------------- PRIMERDIVISOR -----------------------------------
------------------------------------------------------------------------------------
primeDivisors :: Int -> [Int]
primeDivisors a = primeDivisorsRec 2
where
primeDivisorsRec :: Int -> [Int]
primeDivisorsRec n
| n == a + 1 = []
| mod a n == 0 && isPrime n = n : primeDivisorsRec (n+1)
| otherwise = primeDivisorsRec (n+1)
where
isPrime :: Int -> Bool
isPrime 0 = False
isPrime 1 = False
isPrime n = not (hasDivisors (n-1))
where
hasDivisors :: Int -> Bool
hasDivisors 1 = False
hasDivisors x = mod n x == 0 || hasDivisors (x-1)