20% discount for your first project

Get a Quote

Kaprekar Number

Kaprekar Number

Suboor Khan

#Problem solving#Typescript
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.

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.

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));


HomeServicesCase StudiesHire Us