-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path016.js
More file actions
28 lines (23 loc) · 643 Bytes
/
016.js
File metadata and controls
28 lines (23 loc) · 643 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
// What is the sum of the digits of the number 2^1000?
function sumOfDigits(n, pow) {
var digits = [1];
while (pow--) {
var cr = 0;
for (var i = 0; i < digits.length; i++) {
var num = n * digits[i] + cr;
digits[i] = num % 10;
cr = Math.floor(num/10);
}
while (cr > 0) {
digits.push(cr % 10);
cr = Math.floor(cr/10);
}
}
var sum = 0;
while (digits.length) {
sum += digits.shift();
}
return sum;
}
console.log( sumOfDigits(2, 1000) );