blob: 4a60d183ac5be201e58ff4b27ad6af898de5a1a2 (
plain) (
blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
-- Ex1
mymin :: Int -> Int -> Int
mymin a b = if a < b then a
else b
-- Ex2
isInside :: Int -> Int -> Int -> Bool
isInside x a b = a <= x && x <= b
-- Ex3
myfunc :: Double -> Double -> Double
myfunc a b = ((sqrt a) + (sqrt b)) / 2
-- Ex4
myfib :: Int -> Int
myfib n = if n == 1 then 0
else if n == 2 then 1
else myfib (n - 1) + myfib (n - 2)
-- Ex5
mygcd :: Int -> Int -> Int
mygcd a b = if b == 0 then a
else mygcd b (mod a b)
-- Ex6
mymindivisor :: Int -> Int -> Int
mymindivisor x start = if (rem x start) == 0 then start
else mymindivisor x (start + 1)
mymaxdivisor :: Int -> Double
mymaxdivisor x = (fromIntegral x) / (fromIntegral (mymindivisor x 2))
-- Ex7
sumOdd :: Int -> Int -> Int
sumOdd a b = if a > b then 0
else if (mod a 2) == 0 then a + 1 + (sumOdd (a + 3) b)
else a + (sumOdd (a + 2) b)
-- Ex8
isPrime :: Int -> Bool
isPrime x
| x <= 3 = True
| (mod x 2) == 0 || (mod x 3) == 0 = False
| otherwise = calc 6 x (sqrt (fromIntegral x))
where
calc :: Int -> Int -> Double -> Bool
calc i x max
| (fromIntegral (i - 1)) > max = True
| (mod x (i - 1)) == 0 || (mod x (i + 1)) == 0 = False
| otherwise = calc (i + 6) x max
-- Ex9
numLenT10 :: Int -> Int
numLenT10 x = calc x 1
where
calc :: Int -> Int -> Int
calc x res
| x == 0 = res
| otherwise = calc (div x 10) (res * 10)
isPalindrome :: Int -> Bool
isPalindrome x = calc x (div (numLenT10 x) 10)
where
calc :: Int -> Int -> Bool
calc x xT10
| (div x 10) == 0 = True
| (rem x 10) /= (div x xT10) = False
| otherwise = calc (rem (div x 10) xT10) (div xT10 10)
countPalindromes :: Int -> Int -> Int
countPalindromes a b = calc a b 0
where
calc :: Int -> Int -> Int -> Int
calc a b sum
| a > b = sum
| isPalindrome a = calc (a + 1) b (sum + 1)
| otherwise = calc (a + 1) b sum
-- Ex10
sumDivisors :: Int -> Int
sumDivisors x = calc x 0 1 (div x 2)
where
calc :: Int -> Int -> Int -> Int -> Int
calc x sum i max
| i > max = sum
| (rem x i) == 0 = calc x (sum + i) (i + 1) max
| otherwise = calc x sum (i + 1) max
main :: IO ()
main = do
print (mymin 5 6) -- 5
print (isInside 7 (-48) 299) -- True
print (myfunc 25 49) -- 6
print (myfib 8) -- 13
print (mygcd 48 18) -- 6
print (mymaxdivisor 2145) -- 715
print (sumOdd 2 6) -- 8
print (isPrime 6367) -- True
print (countPalindromes 1 100) -- 18
print (sumDivisors 1800) -- 4245
|