Skip to content

Add third solution for the Euler project problem 16. #12777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions project_euler/problem_016/sol3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Problem 16: https://projecteuler.net/problem=16

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?
"""

from numpy import log10, zeros


def solution(power: int = 1000) -> int:
"""
Calculates the sum of digits by explicit construction of digits array
without the help of Python's large integer property.

>>> solution(50)
76
>>> solution(20)
31
>>> solution(15)
26
>>> solution(3)
8
"""

digits = zeros(int(power * log10(2)) + 1)
digits[0] = 1
dig_count = 1
for _ in range(power):
carry = 0
for d in range(dig_count):
digits[d] = digits[d] * 2 + carry
carry = digits[d] // 10
digits[d] %= 10
if carry > 0:
digits[dig_count] = carry
dig_count += 1

return int(sum(digits))


if __name__ == "__main__":
print(solution())