Skip to content
Open
Changes from 4 commits
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
35 changes: 35 additions & 0 deletions maths/special_numbers/kaprekar_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.

A Kaprekar number is one where the square can be split into parts
that sum to the original number.

Args:
n (int): The number to check.

Returns:
bool: True if it's a Kaprekar number, else False.

Examples:
>>> is_kaprekar_number(45)
True
>>> is_kaprekar_number(9)
True
>>> is_kaprekar_number(10)
False
"""
square = str(n**2)
for i in range(1, len(square)):
left, right = square[:i], square[i:]
if int(right) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems this check is not needed anymore.

continue
if n == int(left or "0") + int(right):
return True
return n == 1


if __name__ == "__main__":
import doctest

doctest.testmod()