freeCodeCamp/curriculum/challenges/italian/10-coding-interview-prep/rosetta-code/kaprekar-numbers.md

111 lines
2.3 KiB
Markdown
Raw Normal View History

---
id: 5a23c84252665b21eecc7eca
title: Kaprekar numbers
challengeType: 5
forumTopicId: 302296
dashedName: kaprekar-numbers
---
# --description--
2021-07-10 04:23:54 +00:00
A positive integer is a [Kaprekar number](https://en.wikipedia.org/wiki/Kaprekar number) if:
<ul>
<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>
</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>
<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>
</ul>
# --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.
# --hints--
`isKaprekar` should be a function.
```js
assert(typeof isKaprekar == 'function');
```
`isKaprekar(1, 10)` should return a boolean.
```js
assert(typeof isKaprekar(1, 10) == 'boolean');
```
`isKaprekar(1, 10)` should return `true`.
```js
assert.equal(isKaprekar(1, 10), true);
```
`isKaprekar(9, 10)` should return `true`.
```js
assert.equal(isKaprekar(9, 10), true);
```
`isKaprekar(2223, 10)` should return `true`.
```js
assert.equal(isKaprekar(2223, 10), true);
```
`isKaprekar(22823, 10)` should return `false`.
```js
assert.equal(isKaprekar(22823, 10), false);
```
`isKaprekar(9, 17)` should return `false`.
```js
assert.equal(isKaprekar(9, 17), false);
```
`isKaprekar(225, 17)` should return `true`.
```js
assert.equal(isKaprekar(225, 17), true);
```
`isKaprekar(999, 17)` should return `false`.
```js
assert.equal(isKaprekar(999, 17), false);
```
# --seed--
## --seed-contents--
```js
function isKaprekar(n, bs) {
}
```
# --solutions--
```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) {
b += (a % bs) * s;
a = Math.floor(a / bs);
if (b && a + b == n) return true;
}
return false;
}
```