Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Functional Programming
diff options
context:
space:
mode:
Commit81f851c6ea7988314812cf7cdb3cc94a6aa5cbf3 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-24 11:58:08 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-24 11:58:08 +0200
Filesca343132e3e9009c1d30a94283b2f18b852bacd4 /Year 2/Functional Programming
uni-projects-81f851c6ea7988314812cf7cdb3cc94a6aa5cbf3.tar
uni-projects-81f851c6ea7988314812cf7cdb3cc94a6aa5cbf3.tar.gz
uni-projects-81f851c6ea7988314812cf7cdb3cc94a6aa5cbf3.zip
Parent14281c12f710ee1cb99564201ca0464e79013827 (diff)
[FP/P07] Added work from today's practicum
1 files changed, 111 insertions, 0 deletions
-rw-r--r--Year 2/Functional Programming/07-Practice-24.11.2022.hs111
diff --git a/Year 2/Functional Programming/07-Practice-24.11.2022.hs b/Year 2/Functional Programming/07-Practice-24.11.2022.hs
new file mode 100644
index 0000000..4a60d18
--- /dev/null
+++ b/Year 2/Functional Programming/07-Practice-24.11.2022.hs
@@ -0,0 +1,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