mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
part 2b done :)
This commit is contained in:
+40
-8
@@ -16,7 +16,10 @@ import Data.List
|
||||
-- Hint! pattern matching is your friend.
|
||||
|
||||
binomial :: Integer -> Integer -> Integer
|
||||
binomial = todo
|
||||
binomial n k
|
||||
| k == 0 = 1
|
||||
| n == 0 && k > 0 = 0
|
||||
| otherwise = binomial (n-1) k + binomial (n-1) (k-1)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 2: implement the odd factorial function. Odd factorial is like
|
||||
@@ -27,7 +30,10 @@ binomial = todo
|
||||
-- oddFactorial 6 ==> 5*3*1 ==> 15
|
||||
|
||||
oddFactorial :: Integer -> Integer
|
||||
oddFactorial = todo
|
||||
oddFactorial n
|
||||
| n <= 0 = 1
|
||||
| n `mod` 2 == 0 = oddFactorial (n-1)
|
||||
| otherwise = n * oddFactorial (n-2)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 3: implement the Euclidean Algorithm for finding the greatest
|
||||
@@ -59,7 +65,11 @@ oddFactorial = todo
|
||||
-- * https://en.wikipedia.org/wiki/Euclidean_algorithm
|
||||
|
||||
myGcd :: Integer -> Integer -> Integer
|
||||
myGcd = todo
|
||||
myGcd a b
|
||||
| a == b = a
|
||||
| a > b = myGcd (a-b) b
|
||||
| a < b = myGcd a (b-a)
|
||||
| otherwise = 9999999
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 4: Implement the function leftpad which adds space characters
|
||||
@@ -75,7 +85,9 @@ myGcd = todo
|
||||
-- * you can compute the length of a string with the length function
|
||||
|
||||
leftpad :: String -> Int -> String
|
||||
leftpad = todo
|
||||
leftpad str n
|
||||
| n > length str = leftpad (" " ++ str) n
|
||||
| otherwise = str
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 5: let's make a countdown for a rocket! Given a number, you
|
||||
@@ -91,7 +103,14 @@ leftpad = todo
|
||||
-- * you'll probably need a recursive helper function
|
||||
|
||||
countdown :: Integer -> String
|
||||
countdown = todo
|
||||
countdown n
|
||||
| n > 0 = countdown' n "Ready!!"
|
||||
| otherwise = "Ready! Liftoff!"
|
||||
|
||||
countdown' :: Integer -> String -> String
|
||||
countdown' n str
|
||||
| n > 0 = countdown' (n-1) (str ++ (show n) ++ "... ")
|
||||
| otherwise = str ++ "Liftoff!"
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 6: implement the function smallestDivisor that returns the
|
||||
@@ -109,7 +128,14 @@ countdown = todo
|
||||
-- Hint: remember the mod function!
|
||||
|
||||
smallestDivisor :: Integer -> Integer
|
||||
smallestDivisor = todo
|
||||
smallestDivisor n
|
||||
| n > 1 = smallestDivisor' n 2
|
||||
|
||||
smallestDivisor' :: Integer -> Integer -> Integer
|
||||
smallestDivisor' n t
|
||||
| n > 1 = if n `mod` t == 0 then t
|
||||
else smallestDivisor' n (t+1)
|
||||
| otherwise = 0
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 7: implement a function isPrime that checks if the given number
|
||||
@@ -118,7 +144,10 @@ smallestDivisor = todo
|
||||
-- Ps. 0 and 1 are not prime numbers
|
||||
|
||||
isPrime :: Integer -> Bool
|
||||
isPrime = todo
|
||||
isPrime n
|
||||
| n <= 1 = False
|
||||
| otherwise = if smallestDivisor n == n then True
|
||||
else False
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 8: implement a function biggestPrimeAtMost that returns the
|
||||
@@ -133,4 +162,7 @@ isPrime = todo
|
||||
-- biggestPrimeAtMost 10 ==> 7
|
||||
|
||||
biggestPrimeAtMost :: Integer -> Integer
|
||||
biggestPrimeAtMost = todo
|
||||
biggestPrimeAtMost n
|
||||
| n <= 2 = 2
|
||||
| otherwise = if isPrime n then n
|
||||
else biggestPrimeAtMost (n-1)
|
||||
|
||||
Reference in New Issue
Block a user