Kaprekar Number
A Kaprekar number is a number whose square when divided into two parts and such that sum of parts is equal to the original number and none of the parts has value 0.
Suboor Khan
Software Engineer
Problem solvingTypescript
A Kaprekar number is a number whose square when divided into two parts and such that sum of parts is equal to the original number and none of the parts has value 0.
const kaprekarNumber = (limit: number) => {
const arr = [];
for (let i = 0; i <= limit; i++) {
if (i === kaprekar(i * i)) arr.push(i);
}
return arr;
};
const kaprekar = (num: number) => {
const split = num.toString().split('');
const median = Math.floor(split.length / 2);
const slice = split.slice(0, median);
const slice2 = split.slice(median);
return Number(slice.join('')) + Number(slice2.join(''));
};
console.log(kaprekarNumber(100000));