Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Is it a Munchausen Number?

A Munchausen Number in base \$b\$, also known as a Perfect digit-to-digit invariant or PDDI is a peculiar type of positive integer where the sum of its base-\$b\$ digits raised to themselves is equal to the number itself. They are named for the fictional Baron Munchausen, who apparently hoisted himself up via his own ponytail to save himself from drowning. A related concept is Narcissistic numbers.

For instance, \1ドル\$ is trivially a Munchausen number in every base because \1ドル^1=1\$. Additionally, every positive integer is a base-1 Munchausen number by definition.

More interestingly, \3435ドル\$ is a base-10 Munchausen number because \3ドル^3+4^4+3^3+5^5=3435\$, and in fact is the only other base-10 Munchausen number.

A partial list of Munchausen numbers in every base up to 35 can be found on the OEIS as sequence A166623.

Given a positive integer \$n>0\$, determine if it is a Munchausen number in any base \$b\geq2\$.

Rules

  • Default I/O rules apply, so:
    • Full program or functions are acceptable.
    • Input can be from STDIN, as a function argument, and output can be to STDOUT, as a function return value, etc.
  • Default loopholes apply.
  • The output must be one of two distinct, consistent results. So TRUE is fine for truthy and FALSE is fine for falsy, but you can reverse that or return None for truthy and 1 for falsy or whatever. Please specify the selected results in your answer.
  • Your answer has to work at least theoretically for any positive integer.
  • Munchausen numbers use the convention \0ドル^0=1\$, so \2ドル\$ is a base-2 Munchausen number as \1ドル^1+0^0=2\$. Your code must follow this convention.
  • Explanations are strongly encouraged, even though submissions will most likely use the brute-force search method.
  • Using esoteric languages earns you brownie points since Munchausen was apparently a strange person.

Test Cases

Truthy
1 (all bases)
2 (base 2)
5 (base 3)
28 (base 9 and base 25)
29 (base 4)
55 (base 4)
3435 (base 10)
923362 (base 9)
260 (base 128)
257 (base 64 and base 253)
Falsy
3
4
591912
3163
17

This is , so the shortest answer in each language (in bytes) wins!

Answer*

Draft saved
Draft discarded
Cancel
7
  • \$\begingroup\$ 71 using a for loop \$\endgroup\$ Commented May 11, 2019 at 13:22
  • \$\begingroup\$ @digEmAll Thanks! I shaved off another 2 bytes by using booleans instead of integers. \$\endgroup\$ Commented May 11, 2019 at 20:20
  • \$\begingroup\$ I was trying to understand what you changed to save 2 bytes from mine besides changing + with | and removing !, then I realized I wrote 71 but my code was actually 70 :D \$\endgroup\$ Commented May 12, 2019 at 6:16
  • \$\begingroup\$ Great idea using | BTW ! \$\endgroup\$ Commented May 12, 2019 at 6:19
  • \$\begingroup\$ @digEmAll Oh yes, I didn't even think to check your byte count! :) \$\endgroup\$ Commented May 12, 2019 at 8:37

AltStyle によって変換されたページ (->オリジナル) /