|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||||
|
|||||
|
[Lua] Adding big numbers using strings
I'm trying to figure out the first term in the Fibonacci sequence to have 1000 digits. Now, I know I could use some fancy big number library, but I like to do these problems without any libraries that aren't added automatically. For some reason, my sum() method is giving weird results. Here's my code:
Lua Code:
You'll see that sum("1","1") is gives a result of "12", but I can't figure out why. Can anyone else see it? |
|
#2
|
|||
|
|||
|
Code:
num2.sub(1,dif) |
|
#3
|
|||
|
|||
|
Oh whoops. Well now I have a few new problems:
1. All my numbers returned by sum have leading zeros. Easily fixed by changing return sum to return sum:gsub("0","") 2. Every time I run it, it dies on the 14 term of the sequence. It tells me I'm trying to perform arithmetic on a string value on line 9. Why would it randomly tell me this in the middle of the execution after that method has already been called multiple times? Last edited by dustpyle_x3 : October 10th, 2007 at 11:47 AM. |
|
#4
|
||||
|
||||
|
Quote:
Quote:
|
|
#5
|
|||||
|
|||||
|
Ya, that was my next step after I couldn't find anything that would break it. Then it hit me that sum:gsub("0","") was replacing ALL the zeros. Changed that to tonumber(sum). Then it gave me some other error and I had to change num1,num2 = num2,sum(num1,num2) to num1,num2 = num2,sum(num1,num2).."" and now it's making it to like the 100th term, and then breaking. Same error as before too. Trying to perform arithmetic on a string on line 9. I'll see if I can't find what's wrong.
[edit] Oh wait, I see it. I'll be right back after I fix it. Ok, complete. Final code: Lua Code:
And the answer is 4782 Thanks for your help![/edit] Last edited by dustpyle_x3 : October 11th, 2007 at 07:11 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > [Lua] Adding big numbers using strings |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|