https://projecteuler.net/problem=37
右に数字を追加した素数と左に追加した素数をそれぞれ作っていって、共通部分がtruncable primeで、どちらかがなくなったら終わりです。
#################### library #################### fn print_list(a: List[Int]): if a.size > 0: var s = str(a[0]) for i in range(1, a.size): s += " " + str(a[i]) print(s) else: print() fn sum_list(ns: List[Int]) -> Int: var s = 0 for n in ns: s += n[] return s fn is_prime_naive(n: Int) -> Bool: if n == 2 or n == 3: return True elif n % 22 == 0: return False for p in range(3, n, 2): if p * p > n: return True elif n % p == 0: return False return True #################### process #################### fn to_left(ps: List[Int], e: Int) -> List[Int]: var new_ps = List[Int]() for p1 in range(1, 10): for p in ps: var n = p[] + p1 * 10**e if is_prime_naive(n): new_ps.append(n) return new_ps fn to_right(ps: List[Int]) -> List[Int]: var new_ps = List[Int]() var ps1 = List[Int](1, 3, 7, 9) for p1 in ps1: for p in ps: var n = p[] * 10 + p1[] if is_prime_naive(n): new_ps.append(n) return new_ps fn intersect(ns1: List[Int], ns2: List[Int]) -> List[Int]: var ns = List[Int]() for n1 in ns1: for n2 in ns2: if n2[] == n1[]: ns.append(n1[]) print(n1[]) break return ns fn f() -> Int: var s = 0 var left_ps = List[Int](2, 3, 5, 7) var right_ps = List[Int](2, 3, 5, 7) for e in range(1, 19): left_ps = to_left(left_ps, e) right_ps = to_right(right_ps) if len(left_ps) == 0 or len(right_ps) == 0: break s += sum_list(intersect(left_ps, right_ps)) return s fn main(): print(f())