Advertisement
Vladi1442

Untitled

Apr 2nd, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- More recursion
  2.  
  3. plus1 10 = x + 10 -- first we define add something to x, and then initialize it
  4.     where x = 12
  5.  
  6. plus11 = let x = 11 in x + 23 -- in addition to previous example where x = 12(so we have 12 + 11 = 23)
  7.  
  8. factorial1 0 = 0
  9. factorial1 x = factorial1(x - 1) * x -- it doesn't matter if we first put (factorial1(x - 1)) or x
  10.  
  11. {-- Find m-th summation of first n natural numbers.
  12. If m > 1
  13.   SUM(n, m) = SUM(SUM(n, m - 1), 1)
  14. Else
  15.   SUM(n, 1) = Sum of first n natural numbers.
  16. --}
  17.  
  18. sumOfNaturalNumbers 1 = 1
  19. sumOfNaturalNumbers n = n + sumOfNaturalNumbers(n - 1)
  20.  
  21. sumOfNaturalNumbers1 x = fromIntegral (x * (x + 1) / 2) -- or there's another way for that - div(x * (x + 1) / 2)
  22.  
  23. summation n 1 = sumOfNaturalNumbers n
  24. summation n m = summation (summation n (m - 1)) 1
  25.  
  26. {--
  27. Write function calculateF
  28. calculateF(n) = n, if n < 3
  29. calculateF(n) = calculateF(n - 1) + 2*calculateF(n - 2) + calculateFf(n - 3), otherwise
  30. --}
  31.  
  32. -- here is the first way to do it, with guards(it's more optimal solution)
  33. calculateF n
  34.     | n < 3 = n
  35.     | otherwise = calculateF(n - 1) + 2*calculateF(n - 2) + calculateF(n - 3)
  36.  
  37. -- here is a way to do it with if statements, but it's better with guards
  38.  
  39. calculateF1 n = if n < 3 then n else calculateF1(n - 1) + 2*calculateF1(n - 2) + calculateF1(n - 3)
  40.  
  41. -- Write function coundDigits n a b ,calculating how many time there is
  42. -- the digit n in range of Integers [a,b]
  43.  
  44. countDigitsNumber 0 _ = 0
  45. countDigitsNumber a n
  46.     | mod a 10 == n = 1 + countDigitsNumber(div a 10) n
  47.     | otherwise = countDigitsNumber(div a 10) n
  48.  
  49. countDigits n a b = countDigitsHelper a b 0
  50.     where
  51.     countDigitsHelper a b sum
  52.         | a == (b + 1) = sum
  53.         | a > b = error "Not valid input"
  54.         | otherwise = countDigitsHelper (a + 1) b ((countDigitsNumber a n) + sum)
  55.  
  56. -- Write function calculateSin n by the formula in week03-task.png
  57.  
  58. factorial 1 = 1
  59. factorial x = factorial(x - 1) * x
  60.  
  61. calculateTeylor x n = ((-1) ** n * x ** (2*n+1)) / factorial(2*n+1)
  62.  
  63. calculateSin n x = calculateSinIter 1 n 0
  64.     where
  65.         calculateSinIter begin end sum
  66.             | begin == (end + 1) = sum
  67.             | otherwise = calculateSinIter (begin + 1) end (sum + calculateTeylor x n)
  68.  
  69.  
  70. {--
  71. Calculate the numbers of all n-digit strictly increasing numbers
  72. --}
  73. -- Write function sumOfPrimes finding the sum of all prime numbers in the interval [a, b]
  74. -- 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