Ti8x Z80 ASM Project Euler 01
August 15, 2019
Implementing int24 math, and solving Problem 01
For those that don’t know, Project Euler is a website that provides several math problems, most of which require some programming approach to solve, whether the approach is a clever algorithm or brute force calculation. It’s one of many sites that provides programming problems for its visitors, but it’s the only one I’ve seen that mixes in the mathematics aspect to create some fun (and quite challenging) nerdy puzzles.
The first Project Euler problem is quite easy, and Python code that solves the problem is shorter than the problem statement itself.
Problem 1: Multiples of 3 and 5 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.
print(sum([i for i in range(1000) if i % 3 == 0 or i % 5 == 0]))
By the way, the answer to the problem is
233168, but that’s not very satisfying, and the python code isn’t very enlightening either. I could easily look up all the answers online if I wanted, but my goal is to try to write some assembly code on the calculator that will solve the problem. Hopefully, in the process I’ll realize something about the problem that isn’t immediately obvious that makes the solution easier to code in assembly. That would certainly be more satisfying and enlightening.
One thing to note, however, is that the answer is pretty large. In binary, it’s
0b111000111011010000, or 18 bits. This poses a small problem for the z80, which is a 8-bit processor but has some support for 16-bit arithmetic. This problem will require at least 3 registers or memory locations to store the result and all intermediate values that the resuit takes on during the calculation.
In fact, since I did look up all the answers online to verify that this challenge would be possible on the z80 (most won’t be feasible due to space requirements, but if I couldn’t get past the first problem, I wouldn’t have even bothered). It turns out that all of the answers are within 64-bits, so any 64-bit machine would be able to compute any of them using most languages. That was a good choice on the part of the Project Euler team, because it increases accessibility and compatibility across everyone’s devices and choice of programming language.