이경수 선생님의 수학실험실

problem16 (Power digit sum) 본문

Project Euler

problem16 (Power digit sum)

(이경수) 2019. 2. 14. 09:32

problem16 (Power digit sum)


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?

In Python:


# Power digit sum
import time

startTime = time.time()

strNum = str(pow(2,1000))
numList = [int(num) for num in strNum]
print(sum(numList))

endTime = time.time()
print(endTime - startTime, "seconds")


Run time: 0.0006120204925537109 seconds



In Java:

//Euler16 Power digit sum

package project_euler;


import java.math.BigInteger;


public class Euler16 {

public static void main(String[] args) {

long startTime = System.currentTimeMillis();

double sum = 0;

BigInteger num = BigInteger.valueOf(2).pow(1000);

String numString = num.toString();

for (int i = 0; i < numString.length(); i++) {

sum += Integer.parseInt(numString.charAt(i) + "");

}

System.out.println(sum);

long endTime = System.currentTimeMillis();

System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");

}

}


Run time: 0.006seconds



solution: 1366


[from Project Euler: https://projecteuler.net/problem=16]


'Project Euler' 카테고리의 다른 글

problem18 (Maximum path sum I)  (0) 2019.02.16
problem17 (Number letter counts)  (0) 2019.02.14
Problem 15(Lattice paths)  (0) 2019.02.11
Problem 14(Longest Collatz sequence)  (0) 2019.02.10
Problem 13(Large sum)  (0) 2019.02.10
Comments