It’s good practice to sharpen your Lua skills, and here is a great way to do it.
The projecteuler.net 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
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.
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.
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.
@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 the TI Nspire uses LUA as programming language (got one of those as well) which is pretty awesome
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
c=1000-a-b
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
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 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.
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).