2019-03-21 11:52:35 +05:30
---
id: 5a23c84252665b21eecc7eca
title: Kaprekar numbers
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302296
2019-03-21 11:52:35 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'description' >
A positive integer is a < a href = "https://en.wikipedia.org/wiki/Kaprekar number" > Kaprekar number< / a > if:
< ul >
2019-05-23 13:57:59 +09:00
< li > It is 1, or,< / li >
< li > The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number. < / li >
2019-03-21 11:52:35 +05:30
< / ul >
Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.Example
Kaprekar numbers:
< ul >
2019-05-23 13:57:59 +09:00
< li > < code > 2223< / code > is a Kaprekar number, as < code > 2223 * 2223 = 4941729< / code > , < code > 4941729< / code > may be split to < code > 494< / code > and < code > 1729< / code > , and < code > 494 + 1729 = 2223< / code > < / li >
< li > The series of Kaprekar numbers is known as < a href = "https://oeis.org/A006886" target = "_blank" > A006886< / a > , and begins as < code > 1, 9, 45, 55, ...< / code > < / li >
2019-03-21 11:52:35 +05:30
< / ul >
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'instructions' >
Write a function that takes a number $n$, a base $bs$, and returns true if the number is a Kaprekar number for the given base. Otherwise, the function returns false.
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2019-03-21 11:52:35 +05:30
tests:
- text: < code > isKaprekar</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof isKaprekar == 'function');
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(1, 10)</ code > should return a boolean.
2020-03-30 11:23:18 -05:00
testString: assert(typeof isKaprekar(1, 10) == 'boolean');
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(1, 10)</ code > should return < code > true</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(1, 10), true);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(9, 10)</ code > should return < code > true</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(9, 10), true);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(2223, 10)</ code > should return < code > true</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(2223, 10), true);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(22823, 10)</ code > should return < code > false</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(22823, 10), false);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(9, 17)</ code > should return < code > false</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(9, 17), false);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(225, 17)</ code > should return < code > true</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(225, 17), true);
2019-03-21 11:52:35 +05:30
- text: < code > isKaprekar(999, 17)</ code > should return < code > false</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(isKaprekar(999, 17), false);
2019-03-21 11:52:35 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function isKaprekar(n, bs) {
2020-09-15 09:57:40 -07:00
2019-03-21 11:52:35 +05:30
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'solution' >
```js
function isKaprekar(n, bs) {
if (n < 1 ) return false ;
if (n == 1) return true;
for (var a = n * n, b = 0, s = 1; a; s * = bs) {
2020-03-30 11:23:18 -05:00
b += (a % bs) * s;
2019-03-21 11:52:35 +05:30
a = Math.floor(a / bs);
if (b & & a + b == n) return true;
2020-03-30 11:23:18 -05:00
}
return false;
}
2019-03-21 11:52:35 +05:30
```
< / section >