Created
January 30, 2020 18:14
-
-
Save iansan5653/4f1908c3ccbd987a46adf79c55b4a9ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if (i % 100_000_000 === 0) console.log(i);
Could've saved an operation for each number.