Project Euler Problem 16 Solved

| Comments

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?

Solution

p16.py
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/python
# -*- coding: utf-8 -*-

import math

if __name__ == '__main__':
    str = format(math.pow(2, 1000), 'f')
    sum = 0
    for c in str[:str.index('.')]:
        sum += int(c)
    print sum
p16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
    "fmt"
    "math/big"
    "strconv"
)

func main() {
    num, base := big.NewInt(1), big.NewInt(2)
    for power := 0; power < 1000; power++ {
        num.Mul(num, base)
    }
    result := 0
    for i := 0; i < len(num.String()); i++ {
        bit, _ := strconv.Atoi(num.String()[i : i+1])
        result += bit
    }
    fmt.Println(num.String(), result)
}

I’m the 109044th person to have solved this problem.

Comments