sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 05:17 AM |
So, this weird script that I made for no apparent reason is refusing to work. Apparently, it is attempting the concatenate a nil value, "Number2", on line 40.
NumberToRecord = 10 PiCalculation= {} Table = {} for i = 1, NumberToRecord do Table[i] = {['Answer']=1} end function Check(Value) for i = 1,NumberToRecord do if GetDifferenceFromPi(Value.Num1/Value.Num2) < Table[i].Answer then table.insert(Table, {["Answer"] = GetDifferenceFromPi(Value.Num1/Value.Num2), ['Number1'] = Value.Num1, i, ['Number2'] = Value.Num2}) if #Table > NumberToRecord then table.remove(Table) end return end end end function GetDifferenceFromPi(num) M = num - math.pi if M < 0 then return(tonumber(tostring(num):sub(2,#tostring(M)))) else return M end end
a = 0 for Num1 = 1,100 do for Num2 = 1,Num1 do a = a +1 if tostring(Num1/Num2):sub(1,5) == "3.141" then table.insert(PiCalculation, {['Num1'] = Num1, ['Num2'] = Num2}) end end end print (a) for Index,Value in ipairs(PiCalculation) do Check(Value) end for index = 1,NumberToRecord do print("Index:"..Table[index].Answer.." is the difference between Pi and "..Table[index].Number1.." divided by "..Table[index].Number2) end |
|
|
| Report Abuse |
|
|
|
| 01 Dec 2013 05:28 AM |
| Well, if we knew what you were trying to do, that might help a bit. |
|
|
| Report Abuse |
|
|
|
| 01 Dec 2013 05:31 AM |
| If you really want to find Pi, it equals 3. 1415926535897 9323846264338 3279502884197 1693993751058 2097494459230 7816406286208 9986280348253 4211706798214 8086513282306 6470938446095 5058223172535 9408128481117 4502841027019 3852110555964 4622948954930 3819644288109 7566593344612 8475648233786 7831652712019 0914564856692 3460348610454 3266482133936 0726024914127 3724587006606 3155881748815 2092096282925 4091715364367 8925903600113 3053054882046 6521384146951 9415116094330 5727036575959 1953092186117 3819326117931 0511854807446 2379962749567 3518857527248 9122793818301 1949129833673 3624406566430 8602139494639 5224737190702 1798609437027 7053921717629 3176752384674 8184676694051 3200056812714 5263560827785 7713427577896 0917363717872 1468440901224 9534301465495 8537105079227 9689258923542 0199561121290 2196086403441 8159813629774 7713099605187 0721134 -- rounded up. |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 05:44 AM |
Why on earth would I be doing that? I'm trying to find the 10 division equations between 1*1 and 1000*1000(Though the above script is only to 100) that have answers closest to Pi. I then want to order them from 1-10 depending on how close they are to Pi. |
|
|
| Report Abuse |
|
|
|
| 01 Dec 2013 05:50 AM |
| Ok, well, is this any actual use of this script? Or you just feel like messing with computer's logic? (Just kidding about the computer logic) |
|
|
| Report Abuse |
|
|
|
| 01 Dec 2013 05:52 AM |
| *Is there any actual use of this script |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
| |
|
|
| 01 Dec 2013 01:59 PM |
| The closest to pi is 22/7 (Good for 2 places after decimal) after that it's 333/106 (Good for 5 places after decimal) then 355/113 (Has 6) |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 01 Dec 2013 02:04 PM |
OP wants to find more. God damn, is it that hard to understand?
print("Index:"..Table[index].Answer.." is the difference between Pi and "..Table[index].Number1.." divided by "..Table[index].Number2) Is this line 40? |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 02:05 PM |
| All of the equations that I've seen resulting from earlier versions of this script have at least the first 3 decimals the same as Pi. |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 02:06 PM |
| Yes, that is line 40. Thanks for actually trying to help. |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 01 Dec 2013 02:10 PM |
Your problem is that you are defining string-based variables, but calling them as though they were regular in-table variables. Use this:
print("Index:"..Table[index]['Answer'].." is the difference between Pi and "..Table[index]['Number1'].." divided by "..Table[index]['Number2']) |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 01 Dec 2013 02:10 PM |
| *referencing, not calling. |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 02:18 PM |
Apparently 'Number2' is still be nil value on line 40. Current script;
NumberToRecord = 10 PiCalculation= {} Table = {} for i = 1, NumberToRecord do Table[i] = {['Answer']=1} end function Check(Value) for i = 1,NumberToRecord do if GetDifferenceFromPi(Value.Num1/Value.Num2) < Table[i].Answer then table.insert(Table, i, {["Answer"] = GetDifferenceFromPi(Value.Num1/Value.Num2), ['Number1'] = Value.Num1, ['Number2'] = Value.Num2}) if #Table > NumberToRecord then table.remove(Table) end return end end end function GetDifferenceFromPi(num) M = num - math.pi if M < 0 then return(tonumber(tostring(num):sub(2,#tostring(M)))) else return M end end
a = 0 for Num1 = 1,100 do for Num2 = 1,Num1 do a = a +1 if tostring(Num1/Num2):sub(1,5) == "3.141" then table.insert(PiCalculation, {['Num1'] = Num1, ['Num2'] = Num2}) end end end print (a) for Index,Value in ipairs(PiCalculation) do Check(Value) end for index = 1,NumberToRecord do print("Index: "..index..""..Table[index]['Answer'].." is the difference between Pi and "..Table[index]['Number1'].." divided by "..Table[index]['Number2']) end |
|
|
| Report Abuse |
|
|
MHebes
|
  |
| Joined: 04 Jan 2013 |
| Total Posts: 2278 |
|
|
| 01 Dec 2013 02:24 PM |
I was bored so I redid your thing:
local check = 1000 local closest = {} for num = 1,check do for denom = 1,check do local distance = math.abs(math.pi-num/denom) if #closest == 0 or distance < closest[3] then closest[1] = num closest[2] = denom closest[3] = distance end end end print("The closest fraction to pi I've found was " .. tostring(closest[1]) .. "/" .. tostring(closest[2]) .. ", which equals " .. tostring(closest[1]/closest[2]))
~ Oh, I'm sorry, did I break your concentration? ~ |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 02:42 PM |
^ Not what I wanted Any help with the script that I made? |
|
|
| Report Abuse |
|
|
MHebes
|
  |
| Joined: 04 Jan 2013 |
| Total Posts: 2278 |
|
|
| 01 Dec 2013 02:44 PM |
But like... you're script is needlessly confusing to read, and it accomplishes the same thing as the one I did. Anyways, I'll look at it.
~ Oh, I'm sorry, did I break your concentration? ~ |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 02:49 PM |
| No, is different. And I'm going to work on efficiency and ease of reading when it is actually functioning. |
|
|
| Report Abuse |
|
|
MHebes
|
  |
| Joined: 04 Jan 2013 |
| Total Posts: 2278 |
|
|
| 01 Dec 2013 03:05 PM |
Line 10. table.insert simply adds a new element to a table, it doesn't replace what's already there. Try replacing it with:
Table[i] = {["Answer"] = GetDifferenceFromPi(Value.Num1/Value.Num2), ['Number1'] = Value.Num1, ['Number2'] = Value.Num2}
Or putting 'table.remove(Table,i)' before it.
~ Oh, I'm sorry, did I break your concentration? ~ |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 03:49 PM |
| I'm trying to record more than one result. |
|
|
| Report Abuse |
|
|
MHebes
|
  |
| Joined: 04 Jan 2013 |
| Total Posts: 2278 |
|
|
| 01 Dec 2013 03:56 PM |
If what I did isn't what you want, this isn't the way to go about it. What DO you want, even?
~ Oh, I'm sorry, did I break your concentration? ~ |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
|
| 01 Dec 2013 04:36 PM |
| If you actually read my script, it would be clear that I wanted the top 10 (or more, as defined by a variable) division sums ordered by how close the result of each one was to Pi. |
|
|
| Report Abuse |
|
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
| |
|
sam8985
|
  |
| Joined: 12 Nov 2011 |
| Total Posts: 582 |
|
| |
|