Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. from operator import mul
  2. from functools import reduce
  3.  
  4.  
  5. def factorial1(n):
  6. if n < 0:
  7. raise Exception("Cannot determine factorial for n < 0")
  8. if n <= 1:
  9. return 1
  10. else:
  11. return factorial1(n - 1) * n
  12.  
  13.  
  14. def factorial2(n):
  15. if n < 0:
  16. raise Exception("Cannot determine factorial for n < 0")
  17. else:
  18. result = 1
  19. for i in range(2, n + 1):
  20. result *= i
  21. return result
  22.  
  23.  
  24. def factorial3(n):
  25. if n < 0:
  26. raise Exception("Cannot determine factorial for n < 0")
  27. return reduce(mul, range(1, n + 1), 1)
  28.  
  29.  
  30. def fibonacci1(n):
  31. if n <= 0:
  32. return 0
  33. elif n == 1:
  34. return 1
  35. elif n == 2:
  36. return 1
  37. else:
  38. return fibonacci1(n - 1) + fibonacci1(n - 2)
  39.  
  40.  
  41. def fibonacci2(n):
  42. a = 0
  43. b = 1
  44. c = 1
  45. if n < 0:
  46. raise Exception("Cannot determine fibonacci for n < 0")
  47. elif n == 0:
  48. return a
  49. elif n == 1:
  50. return b
  51. elif n == 2:
  52. return c
  53. else:
  54. for i in range(2, n):
  55. d = b + c
  56. b = c
  57. c = d
  58. return c
  59.  
  60.  
  61. def fibonacci3(n, x=[0, 1]):
  62. for i in range(abs(n)-1):
  63. x = [x[1], sum(x)]
  64. return x[1]*pow(-1, abs(n) - 1) if n < 0 else x[1] if n else 0
  65.  
  66.  
  67. def return_correct_or_throw(val1, val2, val3):
  68. if val1 == val2:
  69. return val1
  70. if val1 == val3:
  71. return val3
  72. if val2 == val3:
  73. return val2
  74. raise Exception("Cannot determine correct value!")
  75.  
  76.  
  77. if __name__ == "__main__":
  78. input = -1
  79. try:
  80. res = return_correct_or_throw(factorial1(input), factorial2(input), factorial3(input))
  81. print("Correct value for factorial({}) is {}".format(input, res))
  82. except Exception as e:
  83. print(str(e))
  84.  
  85. input = 0
  86. try:
  87. res = return_correct_or_throw(factorial1(input), factorial2(input), factorial3(input))
  88. print("Correct value for factorial({}) is {}".format(input, res))
  89. except Exception as e:
  90. print(str(e))
  91.  
  92. input = 1
  93. try:
  94. res = return_correct_or_throw(factorial1(input), factorial2(input), factorial3(input))
  95. print("Correct value for factorial({}) is {}".format(input, res))
  96. except Exception as e:
  97. print(str(e))
  98.  
  99. input = 2.5
  100. try:
  101. res = return_correct_or_throw(factorial1(input), factorial2(input), factorial3(input))
  102. print("Correct value for factorial({}) is {}".format(input, res))
  103. except Exception as e:
  104. print(str(e))
  105.  
  106. input = 10000
  107. try:
  108. res = return_correct_or_throw(factorial1(input), factorial2(input), factorial3(input))
  109. print("Correct value for factorial({}) is {}".format(input, res))
  110. except Exception as e:
  111. print(str(e))
  112.  
  113.  
  114. input = -1
  115. try:
  116. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  117. print("Correct value for fibonacci({}) is {}".format(input, res))
  118. except Exception as e:
  119. print(str(e))
  120.  
  121. input = 0
  122. try:
  123. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  124. print("Correct value for fibonacci({}) is {}".format(input, res))
  125. except Exception as e:
  126. print(str(e))
  127.  
  128. input = 1
  129. try:
  130. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  131. print("Correct value for fibonacci({}) is {}".format(input, res))
  132. except Exception as e:
  133. print(str(e))
  134.  
  135. input = 2.5
  136. try:
  137. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  138. print("Correct value for fibonacci({}) is {}".format(input, res))
  139. except Exception as e:
  140. print(str(e))
  141.  
  142. input = 5
  143. try:
  144. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  145. print("Correct value for fibonacci({}) is {}".format(input, res))
  146. except Exception as e:
  147. print(str(e))
  148.  
  149. input = 10000
  150. try:
  151. res = return_correct_or_throw(fibonacci1(input), fibonacci2(input), fibonacci3(input))
  152. print("Correct value for fibonacci({}) is {}".format(input, res))
  153. except Exception as e:
  154. print(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement