Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- More recursion
- plus1 10 = x + 10 -- first we define add something to x, and then initialize it
- where x = 12
- plus11 = let x = 11 in x + 23 -- in addition to previous example where x = 12(so we have 12 + 11 = 23)
- factorial1 0 = 0
- factorial1 x = factorial1(x - 1) * x -- it doesn't matter if we first put (factorial1(x - 1)) or x
- {-- Find m-th summation of first n natural numbers.
- If m > 1
- SUM(n, m) = SUM(SUM(n, m - 1), 1)
- Else
- SUM(n, 1) = Sum of first n natural numbers.
- --}
- sumOfNaturalNumbers 1 = 1
- sumOfNaturalNumbers n = n + sumOfNaturalNumbers(n - 1)
- sumOfNaturalNumbers1 x = fromIntegral (x * (x + 1) / 2) -- or there's another way for that - div(x * (x + 1) / 2)
- summation n 1 = sumOfNaturalNumbers n
- summation n m = summation (summation n (m - 1)) 1
- {--
- Write function calculateF
- calculateF(n) = n, if n < 3
- calculateF(n) = calculateF(n - 1) + 2*calculateF(n - 2) + calculateFf(n - 3), otherwise
- --}
- -- here is the first way to do it, with guards(it's more optimal solution)
- calculateF n
- | n < 3 = n
- | otherwise = calculateF(n - 1) + 2*calculateF(n - 2) + calculateF(n - 3)
- -- here is a way to do it with if statements, but it's better with guards
- calculateF1 n = if n < 3 then n else calculateF1(n - 1) + 2*calculateF1(n - 2) + calculateF1(n - 3)
- -- Write function coundDigits n a b ,calculating how many time there is
- -- the digit n in range of Integers [a,b]
- countDigitsNumber 0 _ = 0
- countDigitsNumber a n
- | mod a 10 == n = 1 + countDigitsNumber(div a 10) n
- | otherwise = countDigitsNumber(div a 10) n
- countDigits n a b = countDigitsHelper a b 0
- where
- countDigitsHelper a b sum
- | a == (b + 1) = sum
- | a > b = error "Not valid input"
- | otherwise = countDigitsHelper (a + 1) b ((countDigitsNumber a n) + sum)
- -- Write function calculateSin n by the formula in week03-task.png
- factorial 1 = 1
- factorial x = factorial(x - 1) * x
- calculateTeylor x n = ((-1) ** n * x ** (2*n+1)) / factorial(2*n+1)
- calculateSin n x = calculateSinIter 1 n 0
- where
- calculateSinIter begin end sum
- | begin == (end + 1) = sum
- | otherwise = calculateSinIter (begin + 1) end (sum + calculateTeylor x n)
- {--
- Calculate the numbers of all n-digit strictly increasing numbers
- --}
- -- Write function sumOfPrimes finding the sum of all prime numbers in the interval [a, b]
- -- Write function canbePrime n finding if there is a way by remove one of the digits in n the number became a prime.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement