Skip to content

Instantly share code, notes, and snippets.

@sinisterra
Created January 2, 2019 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinisterra/b49c4efcf810497dfb485edfbb35a769 to your computer and use it in GitHub Desktop.
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
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