MojoでProject Euler 16

https://projecteuler.net/problem=16

Problem 13のBigIntegerに掛け算を付け加えただけです。

from math import max
import sys


#################### BigInteger ####################

struct BigInteger(CollectionElement):
    var v: DynamicVector[Int]
    
    fn __init__(inout self, v: DynamicVector[Int]):
        self.v = v
    
    fn __copyinit__(inout self, other: BigInteger):
        self.v = other.v
    
    fn __moveinit__(inout self, owned other: BigInteger):
        self.v = other.v^
    
    fn __add__(self, other: BigInteger) -> BigInteger:
        var v = DynamicVector[Int]()
        var carry = 0
        for i in range(max(self.v.size, other.v.size)):
            let d1 = self.v[i] if i < self.v.size else 0
            let d2 = other.v[i] if i < other.v.size else 0
            let n = d1 + d2 + carry
            let d = n % 10
            v.push_back(d)
            carry = n // 10
        if carry > 0:
            v.push_back(carry)
        return BigInteger(v)
    
    fn __mul__(self, other: Int) -> BigInteger:
        var v = DynamicVector[Int]()
        var carry = 0
        for i in range(self.v.size):
            let n = self.v[i] * 2 + carry
            let d = n % 10
            v.push_back(d)
            carry = n // 10
        if carry > 0:
            v.push_back(carry)
        return BigInteger(v)
    
    fn __str__(self) -> String:
        var s: String = ""
        for i in range(self.v.size-1, -1, -1):
            s += String(self.v[i])
        return s
    
    @staticmethod
    fn create(n: Int) -> BigInteger:
        var v = DynamicVector[Int]()
        v.push_back(n)
        return BigInteger(v)
    
    @staticmethod
    fn parse(s: String) -> BigInteger:
        var v = DynamicVector[Int]()
        for i in range(len(s)-1, -1, -1):
            v.push_back(ord(s[i]) - 48)
        return BigInteger(v)


#################### process ####################

fn sum(v: DynamicVector[Int]) -> Int:
    var s: Int = 0
    for i in range(v.size):
        s += v[i]
    return s

fn f(N: Int) -> Int:
    var n = BigInteger.create(1)
    for i in range(N):
        n = n * 2
    return sum(n.v)

fn main() raises:
    let args = sys.argv()
    let N = atol(args[1])
    print(f(N))