Kaprekar Number Checker - Problem
A Kaprekar number is a special number where if you square it and split the result into two parts, the sum of those parts equals the original number.
For example, 45 is a Kaprekar number because:
- 45² = 2025
- Split: 20 + 25 = 45
Write a function that checks if a given number is a Kaprekar number. The split should divide the squared number into left and right parts of equal or nearly equal length.
Note: If the left part is empty after splitting, treat it as 0.
Input & Output
Example 1 — Classic Kaprekar Number
$
Input:
n = 45
›
Output:
true
💡 Note:
45² = 2025. Split: '2025' → '20' + '25' = 20 + 25 = 45. Sum equals original, so it's a Kaprekar number.
Example 2 — Not a Kaprekar Number
$
Input:
n = 10
›
Output:
false
💡 Note:
10² = 100. Split: '100' → '1' + '00' = 1 + 0 = 1. Sum (1) ≠ original (10), so not a Kaprekar number.
Example 3 — Single Digit Special Case
$
Input:
n = 1
›
Output:
true
💡 Note:
1² = 1. Split: '1' → '' + '1' = 0 + 1 = 1. By definition, 1 is considered a Kaprekar number.
Constraints
- 1 ≤ n ≤ 104
- n is a positive integer
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code