3.2 KiB
3.2 KiB
id | challengeType | title |
---|---|---|
5900f3a31000cf542c50feb6 | 5 | Problem 55: Lychrel numbers |
Description
num
?
NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.
Instructions
Tests
tests:
- text: <code>countLychrelNumbers(1000)</code> should return 13.
testString: assert.strictEqual(countLychrelNumbers(1000), 13, '<code>countLychrelNumbers(1000)</code> should return 13.');
- text: <code>countLychrelNumbers(5000)</code> should return 76.
testString: assert.strictEqual(countLychrelNumbers(5000), 76, '<code>countLychrelNumbers(5000)</code> should return 76.');
- text: <code>countLychrelNumbers(10000)</code> should return 249.
testString: assert.strictEqual(countLychrelNumbers(10000), 249, '<code>countLychrelNumbers(10000)</code> should return 249.');
- text: Your function should count all Lychrel numbers.
testString: assert.strictEqual(countLychrelNumbers(3243), 39, 'Your function should count all Lychrel numbers.');
- text: Your function should pass all test cases.
testString: assert.strictEqual(countLychrelNumbers(7654), 140, 'Your function should pass all test cases.');
Challenge Seed
function countLychrelNumbers(num) {
// Good luck!
return true;
}
countLychrelNumbers(10000);
Solution
const countLychrelNumbers = (size) => {
const numReverse = (num) => {
return Number(num.toString().split('').reverse().join(''));
};
const isPalin = (num) => {
if (numReverse(num) === num) {
return true;
}
return false;
};
let total = 0;
for (let i = 1; i < size; i++) {
let loopCount = 1;
let sum = i;
while (loopCount < 50) {
sum = sum + numReverse(sum);
if (isPalin(sum)) {
break;
} else {
loopCount++;
}
}
if (loopCount === 50) {
total++;
}
}
return total;
}