🛠️ToolsShed

Number Palindrome Checker

Check if a number is a palindrome, find palindromes in a range, and discover interesting palindromic numbers.

12321
is a palindrome
Reversed: 12321

Questions Fréquentes

Implémentation du Code

def is_palindrome(n) -> bool:
    """Check if a number (int or string) is a palindrome."""
    s = str(n)
    return s == s[::-1]

def next_palindrome(n: int) -> int:
    """Find the next palindrome after n."""
    n += 1
    while not is_palindrome(n):
        n += 1
    return n

def find_palindromes_in_range(start: int, end: int) -> list[int]:
    """Find all palindrome numbers in range [start, end]."""
    return [i for i in range(start, end + 1) if is_palindrome(i)]

# Check specific numbers
for num in [12321, 12345, 99999, 100001, 1234321]:
    result = "✓ palindrome" if is_palindrome(num) else "✗ not palindrome"
    print(f"{num}: {result}")

# Find palindromes in range
print("\nPalindromes 100-200:", find_palindromes_in_range(100, 200))
print("Next palindrome after 999:", next_palindrome(999))
print("Next palindrome after 12345:", next_palindrome(12345))

# Lychrel number check (reverse and add)
def reverse_and_add(n: int, steps: int = 50) -> tuple[bool, int]:
    for _ in range(steps):
        n += int(str(n)[::-1])
        if is_palindrome(n):
            return True, n
    return False, n

print("\n196 Lychrel test:", reverse_and_add(196, 100)[0])  # Famous non-palindrome

Comments & Feedback

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