数字回文チェッカー
数字が回文かチェック、範囲内の回文を検索、興味深い回文数を発見します。
✅
12321
is a palindrome
Reversed: 12321
よくある質問
コード実装
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-palindromeComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.