Question for Project Euler 30

Tell us what’s happening:
The code I have here is correct to case n=3 and n=4, but it is not correct for n=5 for some reason. Can someone help identify the error here? (Please ignore the “infinite loop” btw)

function digitnPowers(n) {
var totalSum = 0;
for(let i = 2**n; i<9**(n+1); i++) {
  var numList = String(i).split("");
  var currNum = i;
  for(let j = 0; j<numList.length; j++) {
    currNum -= parseInt(numList[j])**n;
  if(currNum===0) {
    totalSum += i;
return totalSum;


Challenge: Problem 30: Digit n powers

Link to the challenge:

This code is passing for me…

Does my laptop just have a low processing speed…?

You can trim some time by failing early if the sum exceeds the target number.

Adding a new if clause in the for loop kind of exacerbate the problem. I am trying some alternative method. Maybe artificially decrease the range.

No, adding a ‘new clause’ in the loop makes the problem better, if you do it correctly. I know because I’ve done it and it works.

You can also pre-compute the powers of each digit. There are only 10 powers possible.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.