🛠️ToolsShed

Narcissistic Number Checker

Check if a number is narcissistic (Armstrong number) and find all narcissistic numbers up to a given limit.

NarcissisticNumberChecker.findAllTitle

What is a Narcissistic Number?

A narcissistic number (Armstrong number) equals the sum of its digits each raised to the power of the digit count. Example: 153 = 1^3 + 5^3 + 3^3 = 153.

Pertanyaan yang Sering Diajukan

Implementasi Kode

def is_narcissistic(n: int) -> bool:
    """Check if n is a narcissistic (Armstrong) number."""
    digits = str(n)
    power = len(digits)
    return sum(int(d) ** power for d in digits) == n

def find_narcissistic(limit: int) -> list[int]:
    """Find all narcissistic numbers up to limit."""
    return [n for n in range(limit + 1) if is_narcissistic(n)]

# Check specific numbers
for n in [153, 370, 371, 407, 1634, 9474]:
    digits = [int(d) for d in str(n)]
    power = len(str(n))
    breakdown = " + ".join(f"{d}^{power}" for d in digits)
    result = sum(d ** power for d in digits)
    print(f"{n}: {breakdown} = {result} {'✓' if result == n else '✗'}")

# Find all up to 10000
print("\nAll narcissistic numbers up to 10000:")
print(find_narcissistic(10000))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.