Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
Created February 21, 2022 16:42
Show Gist options
  • Save outofmbufs/1c09f6db8d4f7fca30690d04a28ddebf to your computer and use it in GitHub Desktop.
Save outofmbufs/1c09f6db8d4f7fca30690d04a28ddebf to your computer and use it in GitHub Desktop.
Compute OEIS A046253 sequence. For each digit d in a number n, compute d**d; if they sum to n then n is in the sequence.
from functools import lru_cache
@lru_cache
def _powA046253(i, n):
"""Return i**n for two integers i and n. Defines 0**0 to be ZERO."""
r = i
while n > 1:
r *= i
n -= 1
return r
def A046253(n):
"""Return True if n is in OEIS A046253 sequence."""
# https://oeis.org/A046253
# Sum the digits of n each raised to itself as a power.
# If that equals n, the number is in A046253.
# NOTE: For this purpose, 0**0 is defined to be zero.
return sum([_powA046253(d, d) for d in [int(a) for a in str(n)]]) == n
def gen_A046253(lim):
"""Generate A046253 numbers < lim."""
# this is a brute-force search and will be very slow for large lim
for i in range(lim):
if A046253(i):
yield i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment