2.0 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			2.0 KiB
		
	
	
	
	
	
	
	
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName | 
|---|---|---|---|---|
| 5900f3a41000cf542c50feb7 | Problem 56: Powerful digit sum | 5 | 302167 | problem-56-powerful-digit-sum | 
--description--
A googol (10^{100}) is a massive number: one followed by one-hundred zeros; 100^{100} is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, a^b, where a, b < n, what is the maximum digital sum?
--hints--
powerfulDigitSum(3) should return a number.
assert(typeof powerfulDigitSum(3) === 'number');
powerfulDigitSum(3) should return 4.
assert.strictEqual(powerfulDigitSum(3), 4);
powerfulDigitSum(10) should return 45.
assert.strictEqual(powerfulDigitSum(10), 45);
powerfulDigitSum(50) should return 406.
assert.strictEqual(powerfulDigitSum(50), 406);
powerfulDigitSum(75) should return 684.
assert.strictEqual(powerfulDigitSum(75), 684);
powerfulDigitSum(100) should return 972.
assert.strictEqual(powerfulDigitSum(100), 972);
--seed--
--seed-contents--
function powerfulDigitSum(n) {
  return true;
}
powerfulDigitSum(3);
--solutions--
function powerfulDigitSum(n) {
  function sumDigitsOfPower(numA, numB) {
    let digitsSum = 0;
    let number = power(numA, numB);
    while (number > 0n) {
      const digit = number % 10n;
      digitsSum += parseInt(digit, 10);
      number = number / 10n;
    }
    return digitsSum;
  }
  function power(numA, numB) {
    let sum = 1n;
    for (let b = 0; b < numB; b++) {
      sum = sum * BigInt(numA);
    }
    return sum;
  }
  const limit = n - 1;
  let maxDigitsSum = 0;
  for (let a = limit; a > 0; a--) {
    for (let b = limit; b > 0; b--) {
      const curDigitSum = sumDigitsOfPower(a, b);
      if (curDigitSum > maxDigitsSum) {
        maxDigitsSum = curDigitSum;
      }
    }
  }
  return maxDigitsSum;
}