Advertisement
1m1m0

Tetration Calculator

Feb 13th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. -- Tetration Calculator Script with Extended Abbreviations and Infinite Check
  2.  
  3. -- Function to calculate tetration
  4. function tetration(base, exponent)
  5.     local result = 1
  6.     for i = 1, exponent do
  7.         result = base ^ result
  8.     end
  9.     return result
  10. end
  11.  
  12. -- Function to get the abbreviated form of a number
  13. function getAbbreviation(value)
  14.     if value == math.huge or value == -math.huge then
  15.         return value, ""
  16.     end
  17.  
  18.     local abbreviations = {"", "K", "M", "B", "T", "Q", "Qn", "Sx", "Sp", "Oc", "Nn", "Dc",
  19.         "Td", "Qad", "Qid", "Sxd", "Spd", "Ocd", "Nvd", "Vg", "Uvg", "Dvg",
  20.         "Tvg", "Qavg", "Qivg", "Sxvg", "Spvg", "Ocvg", "Nnvg", "Tg", "Ut",
  21.         "Dtg", "Tttg", "Qatg", "Qitg", "Sxtg", "Sptg", "Octg", "Ntrg", "Qag",
  22.         "Uqag", "Dqag", "Tqag", "Qaqg", "Qiqg", "Sxqg", "Spqg", "Ocqg", "Nnqg",
  23.         "Qig", "Uqig", "Dqig", "Tqig", "Qaqig", "Qiqig", "Sxqig", "Spqig", "Ocqig", "Nnqig",
  24.         "Sg", "Usg", "Dsg", "Tsg", "Qasg", "Qisg", "Sxsg", "Spsg", "Ocsg", "Nnsg",
  25.         "St", "Ust", "Dst", "Tst", "Qast", "Qist", "Sxst", "Spst", "Ocst", "Nnst",
  26.         "Og", "Uog", "Dog", "Tog", "Qaog", "Qiog", "Sxog", "Spog", "Ocog", "Nnog",
  27.         "Nog", "Unog", "Dnog", "Tnog", "Qanog", "Qinog", "Sxnog", "Spnog", "Ocnog", "Nnnog",
  28.         "C", "Uc", "Dc", "Tc"}
  29.  
  30.     local idx = 1
  31.  
  32.     while value >= 1000 and idx < #abbreviations do
  33.         value = value / 1000
  34.         idx = idx + 1
  35.     end
  36.  
  37.     return value, abbreviations[idx]
  38. end
  39.  
  40. -- Function to round to the nearest hundredths
  41. function roundToHundredths(num)
  42.     return math.floor(num * 100 + 0.5) / 100
  43. end
  44.  
  45. -- Input values
  46. local baseValue = 5
  47. local exponentValue = 3
  48.  
  49. -- Calculate tetration
  50. local resultValue = tetration(baseValue, exponentValue)
  51.  
  52. -- Check for infinity
  53. if resultValue == math.huge or resultValue == -math.huge then
  54.     print("Tetration Result: Infinity (or -Infinity)")
  55. else
  56.     -- Round to the nearest hundredths
  57.     local roundedResult = roundToHundredths(resultValue)
  58.  
  59.     -- Get abbreviated form
  60.     local abbreviatedValue, abbreviation = getAbbreviation(roundedResult)
  61.  
  62.     -- Print results
  63.     print("Base: " .. baseValue)
  64.     print("Exponent: " .. exponentValue)
  65.     print("Tetration Result: " .. roundedResult .. " (" .. abbreviatedValue .. abbreviation .. ")")
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement