Muenchhausen Numbers
December 6, 2019
A radix-10 number is a Muenchhausen number if it is equal to the sum of its digits, each digit raised to the power of itself. For instance, 3435 is a Muenchhausen number because 3**3 + 4**4 + 3**3 + 5**5 = 3435. Strict Muenchhausen numbers do not permit 0 as a digit, because 0**0 is undefined. Variant Muenchhausen numbers permit 0 as a digit, with 0**0 defined as 0. Muenchhausen numbers also exist with radices other than 10.
Your task is to find all the Muenchhausen numbers in radix-10. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Here is my take on this in Julia (v. 1.1.1): https://pastebin.com/eppLUrZb
The result is this (fairly short) array: 1, 3435, 438579088
Note that I used the strict definition of Muenchhausen numbers, as it makes for a more interesting collection of numbers. Also, I set a limit of 1 billion when searching through the integer space to save time. Besides, it’s quite unlikely that there are any Muenchhausen numbers larger than that.
Have a nice weekend!
Here’s a variation on that algorithm by Chai Wah Wu on http://oeis.org/A166623: generate all digit combinations of length up to radix+2 (this covers the range 2r^r), calculate the digit sum, then see if that sum has the same digits as the original combination – if so, we have a Münchhausen number. Use 0 or 1 for “zeroval” to set the assumed value for 0^0:
That python program runs happily up to radix 15, but I got bored waiting for hexadecimal, so converted it into C++, which finds 1, c76712ffc311e6e and c4ef722b782c26f, along with c4ef722b7820c2f and 123b2309ff572f6b if we put 0^0 = 0 (not the usual convention). Calculations are done with uint64_t which gives just enough bits to check up to 16^16, which is apparently an adequate bound. There is a possibility that the sum might wrap around and we get a false positive, but that doesn’t seem to happen here. Results agree with https://github.com/elizarov/MunchausenNumbers/ which has an interesting meet-in-the-middle algorithm.
Runtime is about 40 seconds with g++ -O3
Python just got finished radix 16, so here are the full results with 0^0 = 1, and numbers in their proper radixes:
0^0 = 1, not undefined.
The situation is complex: https://en.wikipedia.org/wiki/Zero_to_the_power_of_zero
Also, this video can help shed some light on the 0^0 topic: https://youtu.be/UKqoXMyp4_U
Whatever the case, it’s always useful to remember that the Mathematics we is largely based on conventions and assumptions we have made about certain matters (e.g. Euclid’s axiom about the parallel lines). So, regardless of what 0^0 is in reality (if there is a way to access that kind of truth), what we accept it to be may be a matter of debate. In my experience as an engineer / scientist, figuring out a solution to a problem using Math is much more useful than any philosophical discussions about Math properties. I hope that helps. Cheers!
Klong version 20190209
It figures out the first 3 (0, 1, 3435) as soon as I run the code. The last number (438579088) takes 2-3 hours.
Here’s a GMP-based C++ solution. Combinations are visited using a library I wrote, revdoor.
Example usage for some radixes up to 20 follows the code. The radix of the output numbers corresponds to the specified radix.
For the examples I ran, the runtime scaled approximately exponentially, base 4, for each increment in the radix. E.g., radix 16 took 1.5 minutes, radix 17 took 6 minutes, radix 18 took 23 minutes, radix 19 took 91 minutes, and radix 20 took 334 minutes.
Example usage:
Here’s a variant of my solution above, modified to conduct the search in parallel.
GMP and revdoor are the existing dependencies, with OpenMP added for multiprocessing functionality.
Each process explores a different subset of combinations. A combination unranking function is used to map combination indices to the corresponding combinations, which are used to determine where each process should start its search.
Example comparing 16 threads and 1 thread: