W
ho doesn't enjoy a maths puzzle?
I receive a Friday Quiz that always has a maths puzzle for Q18. Sometimes it's something you can't calculate, like the smallest number of postage stamps to make up a particular value. The kind I like best are the ones you can solve with equations and test the answer. Sometimes it's possible to use a spreadsheet or write a computer program.
Today's Q18 is the latter:
18. What is the lowest 3-digit number that increases in value by 75% when you reverse the order of the digits?
My Minstrel the Forth (Jupiter Ace clone) was out and I've been using this a lot recently, writing a game as well as testing my keyboard interface.
Spoiler alert: if you want to work it out for yourself, look away now. The answer is in the screenshots.
There's definitely room for optimisation (the program is listed at the bottom of this post) but the inability to use nested loops made for a clunkier program and more work to get it working properly.
Be interested to hear how much faster the Forth version was, and why this might be!
ReplyDeleteDoesn't seem like there's any shorts cuts other than 'brute force' try a load of values and see if they work.
Good article and use of screen shots.
Thanks Rick. That's easy - Forth is compiled, and if it's a good implementation of Forth, it can run at near-assembly speeds. Looking back at the magazines from '82, this is the way that they marketed the Jupiter Ace (speed). In this case, the BASIC version was elegant though, due to the nested loops and floating point maths. I've since learned about using floats in Forth. that would have helped the Forth case, and a friend of mine did take a different approach to me (rather than looping three variables and constructing the forward and reverse numbers, he used one counter from 100-999 and calculated the reverse version from that) that was a much more elegant Forth solution.
ReplyDelete