Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/kaprekar-numbers.md
Bhanu Pratap Singh Rathore 79394d141f feat(interview-prep): Converting and transferring Rosetta problems (#35487)
* feat(interview-prep): Converting and transferring Rosetta problems

* feat(interview-prep): Fixing challenges

* fix(challenges): Added code tags

* Update knight's-tour.md

Moved text to instructions section and fixed a small typo.

* feat(interview-prep): Update description of Knight's tour

Co-Authored-By: NitronR <bhanur05@gmail.com>

* feat(interview-prep): Update description of Kaprekar numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>

* feat(inteview-prep): Kaprekar numbers description

Added <code> tags.

* fix(interview-prep): Update description Kaprekar Numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>

* fix(interview-prep): Added code tags Kaprekar numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>
2019-03-21 11:52:35 +05:30

3.3 KiB

id, title, challengeType
id title challengeType
5a23c84252665b21eecc7eca Kaprekar numbers 5

Description

A positive integer is a Kaprekar number if:
  • It is 1, or,
  • The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number.
Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.Example Kaprekar numbers:
  • 2223 is a Kaprekar number, as 2223 * 2223 = 4941729, 4941729 may be split to 494 and 1729, and 494 + 1729 = 2223
  • The series of Kaprekar numbers is known as A006886, and begins as 1, 9, 45, 55, ...

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.

Tests

tests:
  - text: <code>isKaprekar</code> should be a function.
    testString: assert(typeof isKaprekar == 'function', '<code>isKaprekar</code> should be a function.');
  - text: <code>isKaprekar(1, 10)</code> should return a boolean.
    testString: assert(typeof isKaprekar(1, 10) == 'boolean', '<code>isKaprekar(1, 10)</code> should return a boolean.');
  - text: <code>isKaprekar(1, 10)</code> should return <code>true</code>.
    testString: assert.equal(isKaprekar(1, 10), true, '<code>isKaprekar(1, 10)</code> should return <code>true</code>.');
  - text: <code>isKaprekar(9, 10)</code> should return <code>true</code>.
    testString: assert.equal(isKaprekar(9, 10), true, '<code>isKaprekar(9, 10)</code> should return <code>true</code>.');
  - text: <code>isKaprekar(2223, 10)</code> should return <code>true</code>.
    testString: assert.equal(isKaprekar(2223, 10), true, '<code>isKaprekar(2223, 10)</code> should return <code>true</code>.');
  - text: <code>isKaprekar(22823, 10)</code> should return <code>false</code>.
    testString: assert.equal(isKaprekar(22823, 10), false, '<code>isKaprekar(22823, 10)</code> should return <code>false</code>.');
  - text: <code>isKaprekar(9, 17)</code> should return <code>false</code>.
    testString: assert.equal(isKaprekar(9, 17), false, '<code>isKaprekar(9, 17)</code> should return <code>false</code>.');
  - text: <code>isKaprekar(225, 17)</code> should return <code>true</code>.
    testString: assert.equal(isKaprekar(225, 17), true, '<code>isKaprekar(225, 17)</code> should return <code>true</code>.');
  - text: <code>isKaprekar(999, 17)</code> should return <code>false</code>.
    testString: assert.equal(isKaprekar(999, 17), false, '<code>isKaprekar(999, 17)</code> should return <code>false</code>.');

Challenge Seed

function isKaprekar(n, bs) {
  // Good luck!
}

Solution

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