Project Euler is a website that lists many mathematical problems that its users solve by coming up with many diverse methods implemented in many different programming languages. Because each programming language has a slightly (or major) difference in how you approach to writing it, oftentimes when sharing solutions to these problems in different languages you'll eventually find more refined or efficient approaches to the solution of each problem that you may not have originally considered, which I find to be exceptionally fun.
I've been using this website since back when I was in high school to pass the time when I had access to a computer at the labs, it's easy enough to find portable compilers or online interpreters for languages that even on a system without administrative access you could very quickly get one running and start working away at the problems on this website. What I especially liked about it was that because the problems were relatively straightforward and mathematical in nature, you could develop algorithms for solving them rather quickly and could even revise your methods when not at the computer while doing other things. Further, because each problem is self-encapsulated you don't need to worry about sinking an enormous amount of time into any given project or worry about dependencies as all you really need is a buffer out and basic language constructs to solve these problems.
Lately as we've been going into the winter months there's been significantly less work to do at the camp, and as a result I often get a lot of time where I'd otherwise be standing around or doing nothing particularly useful. Because my boss is cool he lets me fuck around on the maintenance department computer so I decided to try my hand at solving these again since I'm required to physically be here to get paid even if I'm not doing anything useful. I also thought it would be neat to have an excuse to think about more complex mathematical things again since I don't often have a reason to do so normally.
To start off the thread I solved the first two problems in C which were very simple, so in order to challenge myself a bit I also wrote implementations in the
FALSE programming language, probably my favorite stack-based esoteric language. I'll only share the FALSE programs as the C programs are rather trivial.
Problem 1:1i:
0s:
[1000 3i;*>]
[
s;3i;*+ s:
1000 5i;*>
5i;* 3/ 3* 5i;* =~
&[s;5i;*+ s:]?
i;1+i:
]
#
s;.
Problem 2:0s:
2 1
[$ 4000000 \>]
[
$$ 2/ 2*
=[$s;+ s:]?
\$@+\
]
#
s;.
Both problems deal with determining divisibility, the first in terms of factors and the second in terms of parity. Because FALSE has no modulo operator and can only operate on integers, I decided the easiest approach would be to divide by the check and then multiply by it again, as if the division resulted in a remainder the decimal would be truncated and multiplying by it again would yield a result that is not equal to the beginning number and therefore is not divisible by the check number. I also especially liked how the second program turned out, as I thought it was especially cute how I was able to reduce the Fibonacci calculation on the stack to simply \$@+\, something that takes four lines of C code to accomplish the same thing.
Please feel free to share your algorithms and implementations if you decide to give it a try! It's a very fun thing to collaborate with others on and I'm excited to see what others here come up with.