We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 95fb181 commit cd7e724Copy full SHA for cd7e724
1 file changed
maths/special_numbers/kaprekar_number.py
@@ -0,0 +1,35 @@
1
+def is_kaprekar_number(n: int) -> bool:
2
+ """
3
+ Determine whether a number is a Kaprekar number.
4
+
5
+ A Kaprekar number is one where the square can be split into parts
6
+ that sum to the original number.
7
8
+ Args:
9
+ n (int): The number to check.
10
11
+ Returns:
12
+ bool: True if it's a Kaprekar number, else False.
13
14
+ Examples:
15
+ >>> is_kaprekar_number(45)
16
+ True
17
+ >>> is_kaprekar_number(9)
18
19
+ >>> is_kaprekar_number(10)
20
+ False
21
22
+ square = str(n ** 2)
23
+ for i in range(1, len(square)):
24
+ left, right = square[:i], square[i:]
25
+ if int(right) == 0:
26
+ continue
27
+ if n == int(left or "0") + int(right):
28
+ return True
29
+ return n == 1
30
31
32
+if __name__ == "__main__":
33
+ import doctest
34
35
+ doctest.testmod()
0 commit comments