Skip to content

Instantly share code, notes, and snippets.

@Krinkle
Created April 18, 2019 01:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krinkle/11c0ac4f417005e18fb3231fd3d1e5b3 to your computer and use it in GitHub Desktop.
Save Krinkle/11c0ac4f417005e18fb3231fd3d1e5b3 to your computer and use it in GitHub Desktop.
3435 – The sum of each digit raised to the power of itself.
const MAX = 100 * 1000 * 1000; // 100 million
function powself(number) {
if (number === 0) {
return NaN; // matt does not like zero.
} else {
return Math.pow(number, number);
}
}
function sum(acc, number) {
return acc + number;
}
// The sum of each digit (base10) raised to the power of itself.
function compute(input) {
return input.toString()
.split('')
.map(Number)
.map(powself)
.reduce(sum, 0);
}
let i = 0;
while (i++ <= MAX) {
const result = compute(i);
if (result === i) {
console.log('match:', i);
} else {
// also print close matches that are within 5
if (result >= (i - 5) && result <= (i + 5)) {
if (i <= 9999) {
console.log(`... ${i} computes to ${result}`);
} else {
console.log(`... ${i.toLocaleString()} computes to ${result.toLocaleString()}`);
}
}
}
}
console.log('');
console.log(`Reached ${MAX.toLocaleString()}!`);
$ node parkers-number-of-feb2014.js

match: 1

... 2 computes to 4
... 31 computes to 28
... 32 computes to 31
... 3151 computes to 3154
... 3152 computes to 3157

match: 3435

... 16,824,413 computes to 16,824,417
... 16,824,423 computes to 16,824,420
... 17,648,444 computes to 17,648,440
... 18,471,467 computes to 18,471,472
... 18,471,476 computes to 18,471,472
... 34,378,318 computes to 34,378,313
... 34,378,338 computes to 34,378,339

Reached 100,000,000!

See also Matt Parker's Favourite Number (MathsInsipration).

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