Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. --Take in a num
  2. --and find an equation that approximately matches it
  3. t1 = os.time()
  4. math.randomseed(os.time()/100) -- This ensures that the random vals tested are different on each execution.
  5. num = math.exp(math.pi) -- The target number to approximate
  6. -- 325,841,868 == Current US Population
  7. -- 299792 == Speed of Light (m/s)
  8. -- 3.28084 == Feet in a metre
  9. -- 9.80665 == g
  10. -- 31557600 == Seconds in a year
  11. max = 1000 -- The biggest number tested
  12. opnum = 2 -- Approximately the number of operations used
  13. -- Number of possibilties with 2 operations and a max of 100 = 10^8
  14. it = 5000000 -- Number of random combinations tested
  15. --it = false -- If this line, then it will iterate through ALL possible candidates
  16. -- instead of just a sample of random ones.
  17. function round(x) -- For use with the Lucas Numbers function later
  18. if x-math.floor(x) < 0.5 then return math.floor(x)
  19. else return math.ceil(x) end
  20. end
  21. -- So, ah, lets make an array of all the possible functions
  22. add = function (a) -- This is a different way of defining a function, which I also use for the loadstring() later.
  23. return "+"..test_vals[a]
  24. end
  25. sub = function (a)
  26. return "-"..test_vals[a]
  27. end
  28. mul = function (a)
  29. return "*"..test_vals[a]
  30. end
  31. div = function (a)
  32. return "/"..test_vals[a]
  33. end
  34. exx = function (a)
  35. return "^"..test_vals[a]
  36. end
  37. mod = function (a) -- Not really used, since it's kind of a niche function
  38. return "%"..test_vals[a]
  39. end
  40. lg = function (a) -- This one & all after have to act different
  41. local f = math.random(#char_ops)
  42. return char_ops[f].."math.log("..test_vals[a]..")"
  43. --This makes (SOME NORMAL OPERATION)math.log(a) as a string
  44. --So that I can concat this into a string to be executed
  45. end
  46. squirt = function (a)
  47. local f = math.random(#char_ops)
  48. return char_ops[f].."math.sqrt("..test_vals[a]..")"
  49. end
  50. lucas = function (a,b) -- Lucas Number generator. Easier to use than fibonacci
  51. if b then
  52. local f = math.random(#char_ops)
  53. return char_ops[f].."lucas("..test_vals[a]..")" -- What in self-referenciation
  54. end
  55. local phi = 1.618033981
  56. return round(phi^a) -- See? No need for a For Loop to get the answer, just a singular exp you ceil or floor
  57. end
  58. fact = function (a,b)
  59. if b then
  60. local f = math.random(#char_ops)
  61. return char_ops[f].."fact("..test_vals[a]..")" -- What in self-referenciation
  62. end
  63. if not fct then fct = {1,2,6,24,120,720,5040,40320,363880,3638800,39916800,479001600,6227020800} end
  64. if fct[a] then return fct[a]
  65. else
  66. ans = fct[13]
  67. for y=13,a do
  68. ans = ans * y
  69. end
  70. fct[a] = ans
  71. return ans
  72. end
  73. end
  74. zeta = function(a,b) -- Avast, it's the Riemann Zeta function!
  75. if b then
  76. local f = math.random(#char_ops)
  77. return char_ops[f].."zeta("..test_vals[a]..")" -- What in self-referenciation
  78. end
  79. return 1/(a-1) -- Given that input are positive real numbers
  80. end
  81. shine = function(a)
  82. local f = math.random(#char_ops)
  83. return char_ops[f].."math.sin("..test_vals[a]..")"
  84. end
  85. caus = function(a)
  86. local f = math.random(#char_ops)
  87. return char_ops[f].."math.cos("..test_vals[a]..")"
  88. end
  89. than = function(a)
  90. local f = math.random(#char_ops)
  91. return char_ops[f].."math.tanh("..test_vals[a]..")"
  92. end
  93. sigmoid = function(a)
  94. local f = math.random(#char_ops)
  95. return char_ops[f].."1/(1+math.exp(-"..test_vals[a].."))"
  96. -- This guy is special, since I don't have it call itself
  97. -- Instead it just sends what to evaluate to the compiled string
  98. end
  99. char_ops = {"+","-","*","/","^"} -- All the operations that are simply one symbol, for use with the weirder functions
  100. ops = {add,sub,mul,div,exx,lg,fact,lucas,squirt,zeta,sigmoid,than,caus,shine} -- All the operations that are used by the program to make the approximate
  101. p = (#char_ops*(#ops-#char_ops+1))^opnum*max^(opnum+1)
  102. if it > p then it = p end
  103. best = 7
  104. test_vals,test_ops = {},{}
  105. --[[if ops[1] == function (a,b) return a+b end then print("Yes!") end]]--
  106. -- Doesn't work because they have different function IDs or something.
  107. n = 1
  108. for i=1,it do
  109. for j=1,opnum+1 do -- Make the test constants
  110. test_vals[j] = math.random(max) -- Some number between 1 and max
  111. end
  112. --print(test_vals[1])
  113. for j=1,opnum do -- Make the test operators
  114. a = math.random(#ops) -- The use of #ops allows for an increasable number of supported operations
  115. test_ops[j] = ops[a]
  116. end
  117. -- Now to actually calculate the test value
  118. -- So we assemble a string of this stuff and then execute it using loadstring()
  119. -- Why? Because writing the functions and their order of operations myself would have been painful
  120. -- So why not just have lua do that shit for me?
  121. local s = math.random(2)
  122. local test_str = "return "..test_vals[1]
  123. for j=1,opnum do
  124. test_str = test_str .. test_ops[j](j+1,1)
  125. end
  126. local test = loadstring(test_str)() -- Loadstring actually returns an unnamed function consisting of test_str.
  127. -- This then sets the value for test, to be tested below
  128. if math.abs(test-num) < math.abs(best-num) and test-num ~= 0 then
  129. best = test
  130. best_str = test_str
  131. end
  132. if i/it*100 >= n then
  133. io.write(n .. "%...\n")
  134. n = n + 1
  135. if math.abs(best-num)/num*100 < 0.000001 then break end -- Checks if perfect answer has been found
  136. end
  137. -- I wish I knew how to quit you, Lua.
  138. end
  139. --All possible combinations with the current set-up, given the number of possible numbers used and the number of operations around them
  140. print("All possible combinations from current settings: ",p)
  141. if not it then print("All combinations tested: ",counter) end
  142. io.write("This took ",os.difftime(os.time(),t1)," seconds!\n")
  143. print("Attempted Value:",num)
  144. print("Best result: ",best)
  145. io.write("This one had an inaccuracy of ",math.abs((best-num)/num)*100,"%!\n")
  146. if best_str then io.write(string.sub(best_str,8),"\n") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement