// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (16 votes, average: 5.00 out of 5)
Loading...

Project Euler Solutions

Project Euler 16 Solution

Project Euler 16 Solution

Project Euler 16: Sum of the digits in the number 2^1000


Project Euler 16: Project Euler 16 Problem Description

Project Euler 16: 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?

Analysis

We are asked to calculate a very large power of 2 and sum the individual digits of the result. As Python natively supports arbitrary precision integers, with millions of digits if necessary, it makes this task easy to do.

We calculate 21000 and convert the result to a string: sum(map(int, str(pow(2, 1000)))). Next, using the map function with the result string at the iterator and int as the application function we split the digits of the result into individual characters and convert to integers: sum(map(int, str(pow(2, 1000)))). The sum of the digits is calculated by the sum function as the answer to the problem: sum(map(int, str(pow(2, 1000)))).

The HackerRank Project Euler 16 version increases the exponent to 10,000 and runs a hundred trials in a few milliseconds. Same solution.

Project Euler 16
This program and method
solves all test cases for
Project Euler 16 on HackerRank

Project Euler 16 Solution

Runs < 0.001 seconds in Python 2.7.
download arrowUse this link to get the Project Euler 16 Solution Python 2.7 source.

Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose define to reveal the answer.
|1366|

Afterthoughts

Project Euler 16 Solution last updated

Discussion

No comments yet.

Post a comment