Skip to content

Commit e523f99

Browse files
committed
Clarified Kaprekar number definition and excluded powers of 10 (e.g., 10, 100)
1 parent 50193cd commit e523f99

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
def is_kaprekar_number(n: int) -> bool:
22
"""
3-
Determine whether a number is a Kaprekar number.
3+
Determine whether a number is a Kaprekar number (excluding powers of 10).
44
5-
A Kaprekar number is one where the square can be split into parts
6-
that sum to the original number.
5+
A Kaprekar number is a positive number n such that:
6+
n^2 = q * 10^m + r, for some m >= 1, q >= 0, 0 <= r < 10^m,
7+
and n = q + r, with the restriction that n is not a power of 10.
78
89
Args:
910
n (int): The number to check.
@@ -18,18 +19,19 @@ def is_kaprekar_number(n: int) -> bool:
1819
True
1920
>>> is_kaprekar_number(10)
2021
False
22+
>>> is_kaprekar_number(1)
23+
True
2124
"""
22-
square = str(n**2)
25+
if n == 1:
26+
return True
27+
if n <= 0 or (n % 10 == 0 and n == 10 ** len(str(n))):
28+
return False # Disallow powers of 10 (e.g., 10, 100)
29+
30+
square = str(n ** 2)
2331
for i in range(1, len(square)):
2432
left, right = square[:i], square[i:]
2533
if int(right) == 0:
2634
continue
2735
if n == int(left or "0") + int(right):
2836
return True
29-
return n == 1
30-
31-
32-
if __name__ == "__main__":
33-
import doctest
34-
35-
doctest.testmod()
37+
return False

0 commit comments

Comments
 (0)