3 ms·
Here's a version in Lisp: (defun münchhausen-p (i) (eq (loop for number = i then quotient for (quotient remainder) = (multiple-value-
by zeveb 10y ago
Here's a version in Lisp:
(defun münchhausen-p (i)
(eq (loop for number = i then quotient
for (quotient remainder) = (multiple-value-list (floor number 10))
sum (expt remainder remainder)
until (zerop quotient))
i))
(loop for i from 1 to 500000000 when (münchhausen-p i) do (print i))
It takes .002 seconds to check the first 5,000 integers, 2.108 for the first 5,000,000 and 268.867 seconds for the first 500,000,000.
I'm sure there could be some clever tricks to make it run faster, but this is probably good enough, given that there are, I think, only two such numbers.
- AnimalMuppet 10y agoWell, 9^9 = 387,420,489. So a ten-digit Munchausen number that had enough nines in it could be as big as 3,874,204,890. So going to 500,000,000 isn't quite enough. You can stop at 4 billion, though...
- lispm 10y agoDon't compare numbers with EQ. Use = or EQL.
- zeveb 10y agoYup, you're completely correct. I'm a bit embarrassed.