Advertisement
makispaiktis

Recursive Acts

May 30th, 2021 (edited)
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. from math import log, log2, log10, e
  2.  
  3. # The available calculations are these: log, ln, powers
  4. calcsAvailable = ["log2", "log10", "ln", "e^"]
  5.  
  6. #   Function 1
  7. def recursion(calculation, times, number):
  8.     if calculation not in calcsAvailable:
  9.         print("We cannot perform the act '" + str(calculation) + ")")
  10.         return -1
  11.  
  12.     # Now, we continue
  13.     # Case-0
  14.     if calculation == calcsAvailable[0]:
  15.         result = number
  16.         pretty = ""
  17.         for time in range(int(times)):
  18.             result = log2(result)
  19.             if time != times - 1:
  20.                 pretty += "log2("
  21.             else:
  22.                 pretty += "log2(" + str(number)
  23.         for time in range(int(times)):
  24.             pretty += ")"
  25.         pretty += " = " + str(result)
  26.         print(pretty)
  27.         return result
  28.  
  29.     # Case-1
  30.     if calculation == calcsAvailable[1]:
  31.         result = number
  32.         pretty = ""
  33.         for time in range(int(times)):
  34.             result = log10(result)
  35.             if time != times - 1:
  36.                 pretty += "log10("
  37.             else:
  38.                 pretty += "log10(" + str(number)
  39.         for time in range(int(times)):
  40.             pretty += ")"
  41.         pretty += " = " + str(result)
  42.         print(pretty)
  43.         return result
  44.  
  45.     # Case-2
  46.     if calculation == calcsAvailable[2]:
  47.         result = number
  48.         pretty = ""
  49.         for time in range(int(times)):
  50.             result = log(result)
  51.             if time != times - 1:
  52.                 pretty += "ln("
  53.             else:
  54.                 pretty += "ln(" + str(number)
  55.         for time in range(int(times)):
  56.             pretty += ")"
  57.         pretty += " = " + str(result)
  58.         print(pretty)
  59.         return result
  60.  
  61.  
  62. # MAIN FUNCTION
  63. print()
  64. print(recursion("log2", 4, 2**16))
  65. print()
  66. print(recursion("log10", 2, 10**100))
  67. print()
  68. print(recursion("ln", 2, e**(e**4)))
  69. print()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement