Kaprekar Number | Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Split the square at different points and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 ( These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver code
function iskaprekar ( n ) { if ( n == 1 ) return true ; let sq_n = n * n ; let count_digits = 0 ; while ( sq_n ) { count_digits ++ ; sq_n = parseInt ( sq_n / 10 ) ; } for ( let r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { let eq_parts = Math . pow ( 10 , r_digits ) ; if ( eq_parts == n ) continue ; let sum = parseInt ( ( sq_n1 / eq_parts ) + sq_n1 % eq_parts ) ; if ( sum == n ) return true ; } return false ; } document . write ( " " + " " ) ; for ( let i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) document . write ( i + " " ) ;