# Euler 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}$?

Seriously? Okay, probably more difficult in C.

In [17]:

    s = sum(map(int, str(2**1000)))
    assert(s == 1366)
    print(s)

    1366

Okay, let's do it without big ints.

In [18]:

    def double_number_string(number_string):
        number_string = number_string[::-1] # reverse
        result = []
        carriage = 0
        for c in number_string:
            s = carriage + int(c) + int(c)
            carriage = s // 10
            next_digit = s % 10
            result.append(str(next_digit))
        if carriage > 0:
            result.append(str(carriage))
        result = "".join(result)[::-1]
        return result

    assert(double_number_string("132") == "264")
    assert(double_number_string("965") == "1930")

In [19]:

    def powers_of_two(n):
        """ Retuns nth power of two as a string. """
        assert(n >= 0)
        if n == 0:
            return "1"
        s = "2"
        for _ in range(n - 1):
            s = double_number_string(s)
        return s

    assert(powers_of_two(3) == "8")
    assert(powers_of_two(10) == "1024")

    number_string = powers_of_two(1000)
    print(sum(map(int, number_string)))

    1366

Here we go. The conversion to integer and the sum would be easy in C.