MojoでProject Euler 56

https://projecteuler.net/problem=56

掛け算していくしかないですね。

import sys


#################### List ####################

fn sum_list(a: List[Int]) -> Int:
    var s = 0
    for e in a:
        s += e[]
    return s


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

fn mul_assign(inout ds: List[Int], m: Int):
    var carry = 0
    for i in range(len(ds)):
        var n = ds[i] * m + carry
        ds[i] = n % 10
        carry = n // 10
    
    while carry > 0:
        var d = carry % 10
        ds.append(d)
        carry //= 10

fn f(N: Int) -> Int:
    var max_sum = 0
    for a in range(2, N):
        var ds = List[Int](1)
        for b in range(1, N):
            mul_assign(ds, a)
            var s = sum_list(ds)
            if s > max_sum:
                max_sum = s
    return max_sum

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