Skip to content

Instantly share code, notes, and snippets.

@iansan5653
Created January 30, 2020 18:14
Show Gist options
  • Save iansan5653/4f1908c3ccbd987a46adf79c55b4a9ba to your computer and use it in GitHub Desktop.
Save iansan5653/4f1908c3ccbd987a46adf79c55b4a9ba to your computer and use it in GitHub Desktop.
function getSumOfSquaresOfDigits(n) {
let _n = n;
let result = 0;
while (_n > 0) {
result += (_n % 10) ** 2;
_n = Math.floor(_n / 10);
}
return result;
}
function getConvergence(n) {
let previous = [n];
let _n = n;
while (true) {
let sumOfSquares = getSumOfSquaresOfDigits(_n);
if (previous.includes(sumOfSquares)) {
return sumOfSquares;
}
previous.push(sumOfSquares);
_n = sumOfSquares;
}
}
const convergences = new Set();
for (let i = 1; i < 1_000_000_000; i++) {
if (i % 100_000_000 === 0) console.log(i);
convergences.add(getConvergence(i));
}
console.log(convergences);
@alisnichenko
Copy link

if (i % 100_000_000 === 0) console.log(i); Could've saved an operation for each number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment