MojoでProject Euler 10

https://projecteuler.net/problem=10

エラトステネスの篩をするだけですね。

from math import max, sqrt
import sys

fn make_prime_table(N: Int) -> DynamicVector[Int]:
    let a = DTypePointer[DType.bool].alloc(N+1)
    for i in range(N+1):
        a.store(i, True)
    
    var p: Int = 2
    while p * p <= N:
        if not a.load(p):
            p += 1
            continue
        
        for n in range(p*p, N+1, p):
            a.store(n, False)
        p += 1
    
    var ps = DynamicVector[Int]()
    for n in range(2, N+1):
        if a.load(n):
            ps.push_back(n)
    return ps

fn f(N: Int) -> Int:
    let ps = make_prime_table(N - 1)
    var s: Int = 0
    for i in range(ps.size):
        s += ps[i]
    return s

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