2018-4-2

# Project Euler Problem 16 - Power Digit Sum - solved with Javascript

Solution to the 16th Project Euler Problem - Power Digit Sum - in Javascript.

The 16th Project Euler problem - Power Digit Sum - is stated as follows. 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000?

``````function addIntegerArrays(a = [], b = [], memo = '', carry = 0) {

if (a.length + b.length + carry === 0) {
return memo
}

// Get current number from the end of both
const currentNumberA = a[a.length - 1] || 0;
const currentNumberB = b[b.length - 1] || 0;

// Sum the carry and the last digits or default to 0
const sum = carry + currentNumberA + currentNumberB;

const nextNumber = sum % 10;
const nextCarry = sum > 9 ? 1 : 0;
const nextA = a.slice(0, a.length - 1);
const nextB = b.slice(0, b.length - 1);

const result = `\${nextNumber}\${memo}`;

}

// Convert strings to arrays.
const preparedA = a.split('')
.map(v => parseInt(v, 10));

const preparedB = b.split('')
.map(v => parseInt(v, 10));

}

// Create a range of length N - 1.
const result = new Array(1000 - 1).fill(0)
// Starting at 2, hence N-1, double the value for every step.