Blog 18th October 2024 Collatz conjecture and speed tests Further down this blog are my musings on the remarkable DM41x calculator from Swissmicros... a tour de force of excellence and hard work which captures the entirety of the much loved Hewlett-Packard HP-41 family of RPN calculators. I am a keen RPN enthusiast (Reverse Polish Notation) ever since the tail end of the 1970's when a math teacher at college introduced our class of wide eyed, curious and smart young telecoms students to a beautful and very different HP32e. The HP41 was released in 1979 and was such an immediate hit, it held pride of place right through the 80's and is still used to this day in various legacy applications. I vividly remember the rather novel joy of displaying alpha strings with program inputs and outputs. By 1988 HP launched the pinacle of RPN calculators - the astonishing HP42s which by any standard still remains firmly in a class of its own. This machine had a more advanced programming language than the 41 family, but was backward compatible with any HP41 code - a really strong draw to a great many users. By the time we'd reached the 90's, HP's direction of travel had turned away from dedicated RPN machines to concentrate exclusively on reverse polish lisp (RPL) machines - leading to the release of the HP28, HP49 and HP50 calculators and the really rather good HP48GX in 1993 - a machine that held my hand throughout a 6 year honours degree in Maths and Computer science with the open university. Not every HP customer liked this move to RPL and there are plenty of accounts out there of the sadness of hearing the original and astonishingly talented HP calculator design department had been disbanded and worse subcontracted out to third party company(s). This HP decision to outsource lead directly to the release of the comically awful HP49 and 50 RPL machines (with exceptionally bad keyboards and equally poor quality and feel... I know, because I own both). On balance I didn't mind RPL on the HP48GX, (nor for that matter the completely different HP Prime Programming Language (PPL) and Python used on the 2013 HP Prime) because they both came with extensive graphing support. They're different machines aimed at different markets but both are good at what they do. However whenever I used them I always found myself missing a fully functional, sophisticated but nonetheless dedicated RPN machine. In truth, for the vast bulk of my work a HP41CV or HP42s is really more than sufficient and in fact counter intuitively, these offer a sort of ease of use which the complexity of either a 48 or Prime tends to obscure. Is this just nostalgia? Well, maybe... but perhaps it's also an appreciation of two very good machines. Hewlett-Packard HP42s Calculator HP42s are such a highly regarded machine, they'll routinely command close to 200 UKP on fleabay even for scuffed examples. Mint examples with a box and colour manual can reach north of 315 UKP now, but before these machines became collectable, I found a mint example in Germany back in 2012 complete with original packaging and manual and a HP82240B printer. That printer still works and is fully compatible with both the DM42n and DM41x (although that machine does require extra print support modules).
In terms of potential alternatives to this remarkable machine, there were some open source projects worthy of note, such as the rather splendid WP34s which repurposed a HP financial calculator (the HP30B). The donor machine had to be reflashed and some relatively minor hardware mods completed before fitting a new sticky key label sheet for every key. The WP34s was heavily influenced by the HP42s, but it did an awful lot more while building on support for complex arithmetic, matrix ops, base conversions, stats and all (naturally) fully programmable. It also provided faster hardware than the much older HP42s, a good keyboard experience, an improved screen and even a USB interface for comms with a PC. Although somewhat Heath Robinson in execution the WP34s is still a very capable little RPN machine. I have three mainly because way back when I bought them I saw little realistic prospect of having any other long term RPN alternatives. I've been using one in my garage when turning and machining for many years now, sometimes to convert torques or clearances between imperial and metric or simply to calculate the correct depths of cuts for a part on the lathe. They're good useful machines.
WP34s and HP42s side by side with DM42n
In a lovely parallel thread of development we now come to a quite remarkable software engineer called Thomas Okken. Thomas wrote a fully open source project called Free42 and released version 1.0.0 way back in Nov 2004. His objective was to offer a ground up re-write of the HP42s but without using any of HP's original code; a "re-implementation" if you will but with extra features and additional precision. With good reason, Free42 is often referred to as one of the most accurate and precise calculators in the world because it uses the Intel Decimal Floating-Point Math Library, which is an IEEE 754-2008 compliant suite using quadruple precision decimal floating-point, consuming 16 bytes per number, providing 34 decimal digits of precision with exponents ranging from −6143 to +6144. You can for example calculate nested trig functions such as tan-1(cos-1(sin-1(sin(cos(tan(0.1234)))))) on free42 (or the WP34s) and still end up with 0.1234 in the display. With the original HP42s you'll end up with 0.1232, a small error, but an error nonetheless. Free42 is 100% compatible with the original HP42s so can execute any software written for the original machine. Given the vast HP42s library out in the world, you'll appreciate the advantage and significant achievement that represents. Unsurprisingly, Free42 also offers other enhancements over the original machine. A key feature of its deployment was the release, complete with appropriate skins on iPhone and Andoid devices as well as Mac iOS, Windows and Linux (where I first encountered it). Free42 is (as the name suggests) free across the board. In April 2022 Thomas released an enhanced version of Free42 called Plus42. This extends the display and adds algebraic expressions based on the HP27s, 17B and 19B. It adds unit conversions based on those in the 48 (49 and 50) HP series machines and a folder directory structure similar to the HP48 along with 2D printing and the ability to zoom/pan and find integrals and roots from points on a curve. Plus42 includes financial functions such as TVM and amortization tables. It's open source and while the windows, Mac and Linux versions are free, the iPhone and Android apps are paid apps. We'll explore the Collatz conjecture in a tick - but trust me when I say even a humble iPhone 7 hosting Plus42 is blindingly fast. Swissmicros took Free42 and implemented it on a dedicated hardware platform known as the DM42 which isn't emulated but sits on the SwissMicros OS. One of the lovely design features of this machine are the set of six dedicated keys acting as the original soft keys of the HP42s. The underlying OS handles important features such as firmware upgrades, saving and restoring state (saved as files) as well as saving and loading programs and controlling the USB comms link with the PC. It includes a good file manager with folder support. SwissMicros currently have two versions of this platform for sale. The first is the older DM42 using an ARM cortex-M4F running at 80Mhz (with 32MBit external flash) and a USB B interface. The second is the newer DM42n with an ARM Cortex-M33 CPU running at 160Mhz (with 64 MBit external flash) and a USB C interface. The motivation for these two variants comes down to the discontinuation of the cortex-M4F processor. The 42n is faster and more capable given the extra memory - but is also slightly more expensive, so "pays your money, takes your choice" applies. Otherwise both are essentially the same machine with the same phyical size, same key layout and the same 400x240 pixel screen (using a square pixel pitch at 147µm). Note that the CPUs do run a reduced clock speed when powered by battery in order to conserve power.
  • The DM42 runs at 24Mhz on battery but runs at 80Mhz when powered by USB.
  • The DM42n runs at 48Mhz on battery but runs at 160Mhz when powered by USB.
That said, even when running on battery, these machines are fast especially when compared to an HP42s (which given the age difference, isn't at all surprising). I can certainly recommend the DM42 machines as a highly effective alternative to the HP42s.
Many thanks XKCD - Creative Commons Attribution-NonCommercial 2.5 License.
So what is the Collatz conjecture? Collatz is a simple idea... take any +ve integer as a starting point. If it's even, divide it by 2 to get the next digit. If however it's odd, then multiply it by 3 and add 1 to get your next digit. Repeat for each new digit in the sequence.

Lets do a quick worked example - starting with 10.
  • 10 is even, so divide by 2 to get 5.
  • 5 is odd, so multiply by 3 (to get 15) and add 1 to get 16
  • 16 is even, so divide by two to get 8.
  • 8 is even so divide by 2 to get 4.
  • 4 is even so divide by 2 to get 2.
  • 2 is even so divide by 2 to get 1.
  • 1 is odd so multiply by 3 and add 1, but note you end up back on 4...
...and you'll quickly appreciate once you reach 4, the rules will always lead you back down to 1 and in fact will repeat around that 4,2,1 loop infinitely. The full set of 7 numbers in this specific sequence is 10, 5, 16, 8, 4, 2, 1 (these are often called hailstone numbers due to their habit of bouncing up and down). This sequence took just 7 steps before finalising on 1 but I've seen examples that took 4500 steps. There are also sequences that generate extreme values on route down to 1... for example a starting point of 27 takes 112 steps (including the starting number) to reach 1 - but curiously will generate values as high as 9232 before getting there.
Back in 1937, Lothar Collatz simply asked the question if these rules are applied, will every positive integer eventually end up at 1? His conjecture was they would but thus far no one has been able to mathematically prove the truth of this. Thus far, brute force testing has confirmed that the conjecture is true for positive integers up to 268 (so 2.9515x1020 or if shown as a complete number: 295,147,905,179,352,825,856). However even though this search number is pretty large, compared to the infinite space of positive integers, it is infinitely small. The reality is there is no way to prove this conjecture by a brute force exhaustive search because the search space is infinate. The only way will be to use deductive logic or perhaps some mathematics property discovered in the future. On the other hand, all that would be required to disprove the conjecture would be a single counter example that doesn't reduce to 4.2.1.
The really curious thing is that while a child can easily understand this problem, thus far no one has been able to prove the truth of the underlying conjecture despite lifetimes spent trying to do just that. Jeffrey Lagarias (linked to a great deal of work on this problem) stated that the Collatz conjecture "is an extraordinarily difficult problem, completely out of reach of present day mathematics". The eccentric and quirky Paul Erdős was also quoted as saying "Mathematics is not yet ripe enough for such questions" which was an understated and yet pithy observation. It made me smile when I read it. If you want a good primer on the Collatz Conjecture and which nicely captures the strangeness of the problem - try this:- ...and what does the Collatz conjecture have to do with calculators? Well, calculating a Collatz sequence is computationally intensive and so rather a good way of comparing processing speeds between machines. My objective was to use my own Collatz functions coded on a calculator to compare an original HP42s, a new DM41x, a new DM42n and the Plus42 app running on an iPhone 7 to see how they compared. Both the DM41x and DM42n would be powered by battery (clocks throttled back) but in separate tests, powered via the USB (so clocks at full speed). I built the code using two programs, the first (Collatz) takes a start number in the X register and then computes how many steps the sequence will take to reach 1 using the Collatz rules (and where the count includes the first number provided). Curiously, it did strike me that if that program ever did hang looking for a solution it couldn't find, well we'd have then stumbled across a counter example and therefore shown that the Collatz conjecture was false. Mind you, there isn't much chance of that happening given we'd have to surpase a ceiling of 268. A second program (ColMGR) manages the sequence start value, starting from 1, incrementing after each call to the Collatz program. It monitors how many steps were required for each sequence and records that information in the alpha register in a form SNNN.MAX.STEPS where SNNN is the current sequence start number, MAX is the maximum number of steps recorded across all sequence start values and where the value STEPS are the number of steps required to reach 1 for whatever the current sequence start number is. I started by using a single start value of 3101 (which I know generates a sequence of 155 steps) as the argument for the Collatz function I'd coded. The timing results were
  • HP42s - 26 seconds
  • DM42n (on battery power, reduced clock) - Instant
  • DM42n (on USB power - clocked at full rate) - Instant
  • DM41x (on battery power, reduced clock) - 3 seconds
  • DM41x (on USB power, clocked at full rate) - <1 second
  • iPhone app - Instant
The next step was to run the ColMGR function for 2 minutes on each machine and see how many sequences each could test starting from 1. The results were quite eye opening:-
  • HP42s - Max Sequence start number reached: 41
  • DM42n (on battery power, reduced clock) - Max Sequence start number reached: 3231
  • DM42n (on USB power - clocked at full rate) - Max Sequence start number reached: 6918
  • DM41x (on battery power, reduced clock) - Max Sequence start number reached: 99
  • DM41x (on USB power, clocked at full rate) - Max Sequence start number reached: 212
  • iPhone Plus42 app - Max Sequence start number reached: 163066
The DM42n is very fast... compare 3231 sequences checked when powered by battery (6918 when powered by USB) to just 41 on an original HP42s calculator. Although we're not quite testing like for like (because the DM42n is running Free42 and not Plus42) it is interesting to see Plus42 on a humble iPhone 7 managing to achieve a whopping 163066 checked sequences (50 times more than a battery powered DM42n). Yes, I know these things are all very relative. For a start, the UI of an iPhone calculator app is horrible, with no tactile file (even with haptics enabled). The fact the device also shares function is also a major distraction so any long term use, from my point of view, is a non starter. But it does demonstrate that with the right CPU, the speed of the app can be quite astonishing. Comment | Back to Quick Links...