Skip to content

Add Kaprekar number checker to special_numbers #12723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix Kaprekar logic to exclude powers of 10; add passing tests
  • Loading branch information
Sean-Randall committed May 13, 2025
commit 15c2bac17567a6e9e91007e4c91c67d1c267bb58
8 changes: 5 additions & 3 deletions maths/special_numbers/kaprekar_number.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

Check failure on line 1 in maths/special_numbers/kaprekar_number.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

maths/special_numbers/kaprekar_number.py:1:1: I001 Import block is un-sorted or un-formatted

def is_kaprekar_number(n: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

"""
Determine whether a number is a Kaprekar number (excluding powers of 10).
Expand All @@ -24,10 +26,10 @@
"""
if n == 1:
return True
if n <= 0 or (n % 10 == 0 and n == 10 ** len(str(n))):
return False # Disallow powers of 10 (e.g., 10, 100)
if n <= 0 or math.log10(n).is_integer():
return False # Disallow powers of 10 (e.g., 10, 100, 1000)

square = str(n**2)
square = str(n ** 2)
for i in range(1, len(square)):
left, right = square[:i], square[i:]
if n == int(left or "0") + int(right):
Expand Down
17 changes: 17 additions & 0 deletions tests/special_numbers/test_kaprekar_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from maths.special_numbers.kaprekar_number import is_kaprekar_number

Check failure on line 1 in tests/special_numbers/test_kaprekar_number.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

tests/special_numbers/test_kaprekar_number.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in tests/special_numbers/test_kaprekar_number.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

tests/special_numbers/test_kaprekar_number.py:1:1: INP001 File `tests/special_numbers/test_kaprekar_number.py` is part of an implicit namespace package. Add an `__init__.py`.

def test_kaprekar_numbers():
assert is_kaprekar_number(1) is True
assert is_kaprekar_number(9) is True
assert is_kaprekar_number(45) is True
assert is_kaprekar_number(55) is True
assert is_kaprekar_number(99) is True
assert is_kaprekar_number(297) is True

def test_non_kaprekar_numbers():
assert is_kaprekar_number(10) is False # Power of 10
assert is_kaprekar_number(100) is False # Power of 10
assert is_kaprekar_number(3) is False
assert is_kaprekar_number(50) is False
assert is_kaprekar_number(0) is False
assert is_kaprekar_number(-45) is False
Loading