MojoでProject Euler 55

https://projecteuler.net/problem=55

そのままですね。

import sys


#################### library ####################

fn digits(owned n: Int) -> List[Int]:
    var ds = List[Int]()
    while n > 0:
        var d = n % 10
        ds.append(d)
        n //= 10
    return ds

fn reverse_number(n: Int) -> Int:
    var ds = digits(n)
    var m = 0
    for d in ds:
        m = m * 10 + d[]
    return m


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

fn is_palindromic(n: Int) -> Bool:
    var m = reverse_number(n)
    return m == n

fn next_number(n: Int) -> Int:
    return n + reverse_number(n)

fn is_Lychrel(owned n: Int) -> Bool:
    for _ in range(50):
        n = next_number(n)
        if is_palindromic(n):
            return False
    else:
        return True

fn f(N: Int) -> Int:
    var counter = 0
    for n in range(1, N):
        if is_Lychrel(n):
            counter += 1
    return counter

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