Rust Implementation of Problem 16

View source code here on GitHub!

Problem Solution

pub fn problems::p0016::p0016() -> utils::Answer
 1/*
 2Project Euler Problem 16
 3
 4Problem:
 5
 62**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
 7
 8What is the sum of the digits of the number 2**1000?
 9*/
10use crate::include::utils::Answer;
11
12pub fn p0016() -> Answer {
13    let mut numbers: Vec<u128> = vec![0; 9];
14    let ten36: u128 = 1000000000000000000000000000000000000;
15    numbers[0] = 1;
16    for _ in 0..1000 {
17        for number in numbers.iter_mut() {
18            *number *= 2;
19        }
20        for j in 0..8 {
21            if numbers[j] > ten36 {
22                numbers[j + 1] += numbers[j] / ten36;
23                numbers[j] %= ten36;
24            }
25        }
26    }
27    let mut answer: i128 = 0;
28    let mut power: u128 = 1;
29    for _ in 0..36 {
30        for number in numbers.iter() {
31            answer += ((number / power) % 10) as i128;
32        }
33        power *= 10;
34    }
35    return Answer::Int(answer);
36}

Tags: large-numbers, digit-sum, power