Project Euler: #16 - Power digit sum

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

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?
The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. So what will be the sum of digits of 21000?
While handling bigger power, the resulting large number can not be handled by Javascript.
One approach is to use column multiplication using arrays.
Another approach, which I have opted for is to use BigInt with 2 ** n, where n is the power.
BigInt is a built-in object in JavaScript that represents whole numbers larger than 253.
Note that, We could have used Math.pow() but BigInt can not be used with a Math object as they expect Number as arguments. And BigInt can't accurately convert to a Number. So we settle with 2 ** n.
Note: Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.
const digitSum = n => {
let power = 2n ** BigInt(n);
// Convert BigInt into a String by concatenating with an empty string.
let strNum = '' + power;
return strNum.split('').map(Number).reduce((acc, curr) => acc + curr, 0);
}
console.log(digitSum(1000));
You can find my solution on GitHub 16
For Hackerrank, the above solution works fine for all 10 test cases.
If you have any feedback, please leave it below.
If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!
Reference: