Practising Lua - the Euler problems

It’s good practice to sharpen your Lua skills, and here is a great way to do it.

The site offers over 400 problems which can be solved in under a minute (of computer processing time - not the time it takes you to write a program!). They challenge you to get the right answer, using any language you like. They will simply tell you if you get it right.

Here are the first couple

  1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

  2. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

1 = 233168
2 = 4613732

I did a couple with Matlab once, it’s really fun to do. But using Codea/Lua is more of a challenge, since there aren’t nearly as much predefined math functions.

I don’t even understand the damn questions…

@Ignatz I tried doing a lot of the Euler problems, but I soon found that the math precision wasn’t good enough.

@dave1707 - I thought you would have tried them. This is a situation for your large number arithmetic library!

So… is this like the programmers ACT or SAT?

@Ignatz I can skip the large number library, I can program these on my TI 89 calculator. It’s not as easy to program or as fast as the iPad, but the logic is the same.


I have a TI 84+ and I think the TI series are really cool to progral :wink: the TI Nspire uses LUA as programming language (got one of those as well) which is pretty awesome

Here are a few :slight_smile: solutions, but not in LUA, .

For example one line solution for problem 9 (there is only one Pythagorean triplet, {a, b, c}, for which a + b + c = 1000. Find the product abc).

[ (a,b,c) | a ← [1…500], b ← [1…a-1], c ← [1000-a-b] , c < 500, a^2 + b^2 == c^2 ]

I’d much prefer to debug a Codea program than that horrible line!

Yes, why not, it is question of programmer view.
The line above is simple, it is only about language syntax.
With haskell you can concentrate to problem, not to writing and debugging.
And finally, it was only sample, that with some languages you can quickly and simply write solution for not trivial problem, nothing more.

That is also a brute force solution. There may be an easier way, since if
a+b+c=1000 and aa + bb = cc then
and substituting for c in the second equation, we can simplify to.
1000a + 1000b - ab = 500000
Which means that ab is a multiple of 1000, so at least one of a and b is even, and both a and b are less than 500, narrowing the search.

You can probably analyse further, but that’s as far as my math goes @-)

Yes, but with the conditions on my original line you simply see, what you want to solve.
I tried to run this code on 3 year old AMD 3core proc, but the program really use only one core.
My original line, CPU time: 168ms
Your condition, CPU Time: 46ms

My brain doesn’t do Haskell. That’s why I’m here!

So far I’ve got 5 answers using codea. For some of the longer calculating problems I’ve been using coroutines and a pause option that saves the calculation state so I can continue it at a later time. I’m having a lot of fun with these and I’m learning a lot as well. So far I’ve been able to do #'s 1, 2, 3, 4 and 25 in codea.

I’ve been using this Big number library.

I did Euler 18 with Codea. Found it very interesting. Trying to decide if I want to try Eular 67. That one has 100 rows instead of the 15 like Euler 18.

I tried Eular 67 with Codea. I got an answer of 7273. @Ignatz, are you registered with Euler and can you check if this is correct.

That is correct!

Cool! An interesting problem for solving with Codea is this:

About the one-line-of-code solution mentioned above, I fully agree, because some great math problems can be easily solved even with “esoteric” languages like FALSE (I’ve tried it myself and it is a LOT of fun to do it) with just a couple of lines of code (check for instance the Rosetta Project).

m^2 + mn = m(m+n) = 500 < 625, therefor m = 20, n = 5