Created
January 2, 2019 20:14
-
-
Save sinisterra/b49c4efcf810497dfb485edfbb35a769 to your computer and use it in GitHub Desktop.
Find the first N combinations of n and m where the sum of the digits of n^m equal n
This file contains 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
const { range, sum } = require('lodash') | |
// find the first N combinations of n and m where the sum of the digits of n^m equal n | |
const run = (count) => { | |
let results = [] | |
const digits = n => [...String(n)].map(Number) | |
for (const n of range(2, 300)) { | |
for (const m of range(2, 40)) { | |
const power = Math.pow(n, m) | |
const powerDigits = digits(power) | |
const powerDigitAddition = sum(powerDigits) | |
if (n === powerDigitAddition) { | |
const r = { x:n, y:m, power } | |
console.log(`${n}, ${m}`) | |
results.push(r) | |
if (results.length === count) { | |
return results | |
} | |
} | |
} | |
} | |
} | |
const output = run(50) | |
console.log(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment