Practising Lua - the Euler problems

“Syntax error” :smiley:

I was looking thru some of the Euler problems. There are a lot of them that I don’t know what they’re even talking about, let alone trying to solve them.

Some years ago (when I was probably smarter than I am now) I did the first 80 or so problems in Excel VBA, so I can dig that out for anyone who gets stuck.

But I found that as I went on, the solutions increasingly involved finding some obscure mathematical formula, and were less of a programming challenge than a google search.

Lol, +1 :-j

The interesting thing about it is that you can gradually build your own math library. Perhaps you’ll never use it again, but merely the fact of coding all that sh*t is the real earning… I think, it is something like drinking beer. :smiley: ¡Salud por eso! (Yes, it is weekend)

I’ve already computed factorial of 100 in less than a second with my iPad 2b.C.! (BC=before our era :smiley: )
It’s been a lot of fun to code addition and multiplication of very big numbers by converting them into strings together with basic manipulation of such strings. I recommend to beginners like me to try that. You’ll learn a lot about coding and math. The key was absolutely the ability of Lua to convert strings into numbers at run time! I loved it! :-B

@quezadav Did you do something like this, or did you calculate and display all 158 digits.

100!   9.3326215443944e+157
function setup()
    a="1"
    for z=2,100 do
        a=a*z
    end
    print(a)
end

Yes, I had a lot of fun making “big math” functions

@dave1707, I got the whole bunch of digits! :stuck_out_tongue:

Here’s a version I have that uses a table and prints all the digits. This is set to run 2000! .

function setup()
    local fact=2000
    local a={1}
    for s=2,fact do
        local carry=0
        for z=#a,1,-1 do
            local b=a[z]*s+carry
            if b>9 then
                carry=b//10
                a[z]=b%10
            else
                carry=0
                a[z]=b  
            end   
        end
        while carry>0 do
            table.insert(a,1,carry%10)
            carry=carry//10
        end
    end
    print(#a,"digits ")
    print(fact.."! = ",table.concat(a))
end

:-bd

My code has more lines and is more middle-aged than yours :frowning:
No, it’s fine. Your codes give me more hints how to program. Thanks.

@quezadav That’s how I learn. I study code written by people who do things better than I do.

@quezadav On my iPad Air, the program I wrote above takes 257 seconds to run 20,000! . Here’s an updated version of that program that runs 20,000! in 11 seconds. That’s 23 times faster.

EDIT: Program corrected to display correct value. 20,000! has 77,338 digits. This printed out the correct count, but I didn’t verify each digit.

function setup()
    t=os.time()
    limit=99999999999999
    local fact=20000
    local a={1}
    for s=2,fact do
        local carry=0
        for z=#a,1,-1 do
            local b=a[z]*s+carry
            if b>limit then
                carry=b//(limit+1)
                a[z]=b%(limit+1)
            else
                carry=0
                a[z]=b  
            end   
        end
        while carry>0 do
            table.insert(a,1,carry%(limit+1))
            carry=carry//(limit+1)
        end
    end
    size=string.len(a[1])
    for z=2,#a do
        str="0000000000000000"..a[z]
        a[z]=string.sub(str,string.len(str)-13)
        size=size+string.len(a[z])
    end 
    print("Number of digits ",size)   
    print(fact.."! = ",table.concat(a))    
    print(os.time()-t,"seconds")
end

My code above isn’t working properly. It’s dropping some digits in the middle somewhere. I’ll have to figure out why. I get 72190 digits which is several thousand short of what it should be.

@dave1707, is there and way to create Folders in GitHub?

*any

@TokOut I don’t know the answer to your question but I’m sure someone else will answer. If you make a spelling error in a post, you can tap the gear at the upper right of the post and select edit to change anything in it. You don’t need to create another post to point out the spelling error.

The problem with my code above is, if the result of a calculation is over the limit, then the calculation a[z]=b%(limit+1) will ignore any leading 0’s in the answer. When the calculation is done and the answer is printed, then any table position that should have leading 0’s won’t show them. When comparing calculated answers with actual answers, there will be 0’s missing.

@dave1707, yes, I’ve had also strange results trying to do “digits”- arithmetic above a certain limit of them.

Corrected the above program to print the correct digits for 20,000 ! . There are 77,338 digits which is the correct count, but I didn’t verify each digit.