15 programs measured with axle bench: euler10_sum_primes_sieve, euler12_divisor_counts, euler14_longest_collatz, euler1_multiples_sum, euler20_factorial_digit_sum, euler21_amicable_sum, euler23_non_abundant_sums, euler26_reciprocal_cycles, euler27_quadratic_primes, euler31_coin_ways_dp, euler34_digit_factorials, euler3_largest_prime_factor, euler4_palindrome_products, euler7_nth_prime, euler9_pythagorean_triplets. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.
Benchmarks for v0.23.0
Sieve of Eratosthenes below 27M — strided composite marking plus a linear prime-sum sweep.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 261 ms | 106.0 MB | 105 ms | 97.1 MB | 16 KB |
| Axle -O3 | 262 ms | 105.3 MB | 87 ms | 123.4 MB | 308 KB |
| Rust -O2 | 262 ms | 104.7 MB | 113 ms | 144.5 MB | 3863 KB |
| Axle -O2 | 263 ms | 105.3 MB | 85 ms | 123.0 MB | 308 KB |
| C++ -O2 | 264 ms | 105.9 MB | 107 ms | 97.2 MB | 16 KB |
| Rust -O3 | 264 ms | 104.6 MB | 113 ms | 145.5 MB | 3863 KB |
| Axle -O1 | 271 ms | 105.3 MB | 75 ms | 123.5 MB | 307 KB |
| C++ -O1 | 271 ms | 106.0 MB | 99 ms | 95.9 MB | 16 KB |
| Rust -O1 | 280 ms | 104.7 MB | 105 ms | 141.3 MB | 3863 KB |
| Axle -O0 | 361 ms | 105.3 MB | 69 ms | 123.1 MB | 307 KB |
| C++ -O0 | 372 ms | 106.0 MB | 91 ms | 89.6 MB | 16 KB |
| Rust -O0 | 551 ms | 104.7 MB | 92 ms | 138.1 MB | 3870 KB |
// Project-Euler #10 generalised — sum of all primes below N via the
// Sieve of Eratosthenes.
//
// A flat byte array of N entries (0 = prime candidate, 1 = composite) is
// swept: for each i with i*i < N that is still a candidate, every
// multiple i*i, i*i+i, … < N is marked composite. A final linear sweep
// sums every index still flagged prime into an i64. This stresses
// sequential write bandwidth over a 60 MB array plus the cache-unfriendly
// strided marking pattern.
//
// Index variables are i32 (every value — including the largest stride
// start i*i and the largest index N-1 — stays below 2^31); only the prime
// sum is i64 (it reaches ~1.6e14). Same shape in the {c,cpp,rs} siblings.
//
// Equivalent to euler10_sum_primes_sieve.{c,cpp,rs} — same N, same sieve
// bounds (i*i < N), same i64 sum, same printed value.
fn main() : i32 {
let N : i32 = 27000000;
// sieve[k] == 0 means "k is still a prime candidate"; 1 means
// "composite". Calloc-zeroed, so every entry starts as a candidate.
let mut sieve : i32[27000000] = i32[27000000];
// Mark composites. 0 and 1 are not primes — flag them so the final
// sweep skips them.
sieve[0] = 1;
sieve[1] = 1;
let mut i : i32 = 2;
while (i * i < N) {
if (sieve[i] == 0) {
let mut j : i32 = i * i;
while (j < N) {
sieve[j] = 1;
j = j + i;
}
}
i = i + 1;
}
// Sum every index still flagged as prime. The accumulator is i64
// because the total overflows i32.
let mut sum : i64 = 0;
let mut k : i32 = 2;
while (k < N) {
if (sieve[k] == 0) {
sum = sum + (k as i64);
}
k = k + 1;
}
println(sum);
return 0;
}Divisor counts of the first 13000 triangular numbers by trial division — integer div/mod throughput.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 132 ms | 45.5 MB | 94 ms | 96.2 MB | 16 KB |
| C++ -O3 | 132 ms | 45.5 MB | 93 ms | 96.2 MB | 16 KB |
| Axle -O2 | 161 ms | 45.5 MB | 80 ms | 123.3 MB | 305 KB |
| Axle -O3 | 161 ms | 45.5 MB | 81 ms | 123.5 MB | 305 KB |
| Rust -O3 | 161 ms | 45.5 MB | 93 ms | 134.8 MB | 3862 KB |
| Rust -O2 | 162 ms | 45.5 MB | 94 ms | 135.7 MB | 3862 KB |
| C++ -O1 | 361 ms | 45.5 MB | 92 ms | 95.9 MB | 16 KB |
| Rust -O1 | 395 ms | 45.5 MB | 93 ms | 134.3 MB | 3862 KB |
| C++ -O0 | 413 ms | 45.5 MB | 88 ms | 89.4 MB | 16 KB |
| Axle -O1 | 417 ms | 45.5 MB | 75 ms | 123.3 MB | 305 KB |
| Axle -O0 | 481 ms | 45.5 MB | 70 ms | 123.0 MB | 305 KB |
| Rust -O0 | 523 ms | 45.5 MB | 87 ms | 133.2 MB | 3865 KB |
// Project-Euler #12 generalised — divisor counts of triangular numbers.
//
// For the first M triangular numbers T(k) = k(k+1)/2 (k = 1..M), count
// the divisors of each T(k) by trial division up to sqrt(T). Track the
// MAXIMUM divisor count seen and accumulate an i64 checksum = sum of all
// divisor counts. The trial-division kernel (while d*d <= t { if t%d==0
// then count += two }) is the hot loop: integer division / modulo
// throughput over the first M triangular numbers.
//
// Equivalent to euler12_divisor_counts.{c,cpp,rs} — same M, same i64
// accumulation, same two printed lines (max divisor count, checksum).
fn divisor_count(t : i64) : i64 {
let mut count : i64 = 0;
let mut d : i64 = 1;
while (d * d <= t) {
if (t % d == 0) {
if (d * d == t) {
count = count + 1;
} else {
count = count + 2;
}
}
d = d + 1;
}
return count;
}
fn main() : i32 {
let M : i64 = 13000;
let mut max_count : i64 = 0;
let mut checksum : i64 = 0;
let mut k : i64 = 1;
while (k <= M) {
let t : i64 = k * (k + 1) / 2;
let c : i64 = divisor_count(t);
checksum = checksum + c;
if (c > max_count) {
max_count = c;
}
k = k + 1;
}
println(max_count);
println(checksum);
return 0;
}Longest Collatz chain under 2e6, computed without memoization (compute-bound).
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O1 | 221 ms | 43.8 MB | 75 ms | 124.6 MB | 303 KB |
| Axle -O2 | 232 ms | 43.8 MB | 79 ms | 124.9 MB | 303 KB |
| Axle -O3 | 232 ms | 43.8 MB | 80 ms | 123.3 MB | 303 KB |
| Rust -O2 | 232 ms | 43.8 MB | 91 ms | 129.3 MB | 3862 KB |
| Rust -O1 | 234 ms | 43.8 MB | 91 ms | 131.6 MB | 3862 KB |
| Rust -O3 | 235 ms | 43.8 MB | 91 ms | 131.0 MB | 3862 KB |
| C++ -O2 | 282 ms | 43.8 MB | 93 ms | 96.0 MB | 16 KB |
| C++ -O3 | 282 ms | 43.8 MB | 94 ms | 96.2 MB | 16 KB |
| C++ -O1 | 506 ms | 43.8 MB | 91 ms | 95.8 MB | 16 KB |
| Axle -O0 | 972 ms | 43.8 MB | 70 ms | 125.0 MB | 303 KB |
| Rust -O0 | 4.34 s | 43.8 MB | 86 ms | 131.0 MB | 3865 KB |
| C++ -O0 | 4.43 s | 43.8 MB | 87 ms | 89.4 MB | 16 KB |
// Project-Euler #14 generalised — longest Collatz chain under N.
//
// For every starting number s in [1, N) the Collatz chain length is
// computed WITHOUT memoization: n -> n/2 when even, 3n+1 when odd, until
// n reaches 1. The running value is an i64 because the 3n+1 step
// overflows i32 well before N. The starting number with the longest
// chain is tracked with a strict `>` (so the smallest such starter wins
// on ties — identical tie-break in all four languages).
//
// Two lines are printed: the best starting number, then its chain length.
//
// Equivalent to euler14_longest_collatz.{c,cpp,rs} — same N, same
// no-memoization kernel, same i64 chain value, same tie-break, same two
// printed lines.
fn collatz_length(start : i64) : i64 {
let mut n : i64 = start;
let mut steps : i64 = 0;
while (n > 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
steps = steps + 1;
}
return steps;
}
fn main() : i32 {
let N : i64 = 2000000;
let mut best_start : i64 = 1;
let mut best_len : i64 = 0;
let mut s : i64 = 1;
while (s < N) {
let len : i64 = collatz_length(s);
if (len > best_len) {
best_len = len;
best_start = s;
}
s = s + 1;
}
println(best_start);
println(best_len);
return 0;
}Sum of all multiples of 3 or 5 below 2.5e8 via an explicit modulo scan (no closed-form shortcut).
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 113 ms | 45.7 MB | 94 ms | 96.5 MB | 16 KB |
| C++ -O2 | 114 ms | 45.7 MB | 96 ms | 96.7 MB | 16 KB |
| Axle -O3 | 115 ms | 45.7 MB | 78 ms | 125.4 MB | 303 KB |
| Rust -O3 | 116 ms | 45.7 MB | 91 ms | 133.9 MB | 3862 KB |
| Rust -O2 | 117 ms | 45.7 MB | 92 ms | 134.5 MB | 3862 KB |
| Axle -O2 | 119 ms | 45.7 MB | 78 ms | 123.3 MB | 303 KB |
| Rust -O1 | 257 ms | 45.7 MB | 90 ms | 133.5 MB | 3862 KB |
| C++ -O1 | 489 ms | 45.7 MB | 91 ms | 95.4 MB | 16 KB |
| Axle -O1 | 704 ms | 45.7 MB | 73 ms | 125.3 MB | 303 KB |
| Axle -O0 | 1.12 s | 45.7 MB | 70 ms | 123.1 MB | 303 KB |
| C++ -O0 | 4.64 s | 45.7 MB | 87 ms | 89.0 MB | 16 KB |
| Rust -O0 | 5.37 s | 45.7 MB | 86 ms | 131.4 MB | 3864 KB |
// Project-Euler #1 generalised — sum of all multiples of 3 or 5 below N.
//
// Computed with an EXPLICIT scan over [1, N): each candidate is tested
// with two `% == 0` checks and conditionally added to an i64 running sum.
// The closed-form arithmetic-series shortcut is deliberately NOT used —
// the point is to benchmark the loop + modulo throughput, not the maths.
//
// Equivalent to euler1_multiples_sum.{c,cpp,rs} — same N, same explicit
// loop, same i64 accumulation, same printed line.
fn main() : i32 {
let N : i64 = 250000000;
let mut sum : i64 = 0;
let mut i : i64 = 1;
while (i < N) {
if (i % 3 == 0 || i % 5 == 0) {
sum = sum + i;
}
i = i + 1;
}
println(sum);
return 0;
}Digit sum of 10000! held as a little-endian decimal big-integer — ~1.8e8 steps of schoolbook multiply-by-small-int with an explicit carry chain over a 35660-digit array.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 399 ms | 45.4 MB | 156 ms | 97.2 MB | 16 KB |
| C++ -O3 | 401 ms | 45.4 MB | 159 ms | 97.2 MB | 16 KB |
| C++ -O1 | 404 ms | 45.4 MB | 150 ms | 96.1 MB | 16 KB |
| Rust -O2 | 424 ms | 45.4 MB | 174 ms | 144.5 MB | 3863 KB |
| Rust -O1 | 430 ms | 45.4 MB | 157 ms | 142.9 MB | 3863 KB |
| Rust -O3 | 431 ms | 45.4 MB | 173 ms | 144.5 MB | 3863 KB |
| Axle -O2 | 463 ms | 45.4 MB | 145 ms | 125.4 MB | 305 KB |
| Axle -O3 | 463 ms | 45.4 MB | 147 ms | 126.7 MB | 305 KB |
| Axle -O1 | 494 ms | 45.4 MB | 126 ms | 125.2 MB | 305 KB |
| Axle -O0 | 1.23 s | 45.4 MB | 111 ms | 128.7 MB | 305 KB |
| C++ -O0 | 1.70 s | 45.4 MB | 137 ms | 89.6 MB | 16 KB |
| Rust -O0 | 3.67 s | 45.4 MB | 150 ms | 141.6 MB | 3871 KB |
// Project-Euler #20 generalised — digit sum of N! for a large N.
//
// N! is built in a little-endian decimal big-integer: a flat i32 array
// of one decimal digit per slot, multiplied in place by k = 2, 3, …, N
// with an explicit carry chain. The kernel is that schoolbook
// multiply-by-small-int — one multiply plus a `% 10` / `/ 10` carry split
// per digit, over an array that grows to 35660 digits for N = 10000, so
// roughly 1.8e8 digit steps in total.
//
// Two lines are printed: the number of decimal digits of N!, then the sum
// of those digits.
//
// The digit array is sized 40000 in all three languages — comfortably
// above the 35660 digits of 10000! — so no language reallocates mid-run.
//
// Equivalent to euler20_factorial_digit_sum.{cpp,rs} — same N, same
// digit-array capacity, same carry loop, same two printed lines.
fn main() : i32 {
let N : i32 = 10000;
// Little-endian decimal digits of the running factorial; starts at 1.
let mut digits : i32[40000] = i32[40000];
digits[0] = 1;
let mut len : i32 = 1;
let mut k : i32 = 2;
while (k <= N) {
// digits *= k, propagating the carry across the live prefix.
// digits[i] * k + carry peaks at 9 * 10000 + 9999 — well inside i32.
let mut carry : i32 = 0;
let mut i : i32 = 0;
while (i < len) {
let v : i32 = digits[i] * k + carry;
digits[i] = v % 10;
carry = v / 10;
i = i + 1;
}
while (carry > 0) {
digits[len] = carry % 10;
carry = carry / 10;
len = len + 1;
}
k = k + 1;
}
let mut sum : i64 = 0;
let mut j : i32 = 0;
while (j < len) {
sum = sum + (digits[j] as i64);
j = j + 1;
}
println(len);
println(sum);
return 0;
}Sum of amicable numbers below 150K via paired proper-divisor-sum trial division.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 323 ms | 45.1 MB | 142 ms | 96.2 MB | 16 KB |
| C++ -O2 | 329 ms | 45.1 MB | 139 ms | 96.2 MB | 16 KB |
| Axle -O3 | 347 ms | 45.1 MB | 128 ms | 127.3 MB | 305 KB |
| Axle -O2 | 371 ms | 45.1 MB | 128 ms | 125.2 MB | 305 KB |
| Rust -O2 | 405 ms | 45.1 MB | 149 ms | 135.1 MB | 3862 KB |
| Rust -O3 | 414 ms | 45.1 MB | 146 ms | 133.4 MB | 3862 KB |
| C++ -O1 | 695 ms | 45.1 MB | 135 ms | 95.8 MB | 16 KB |
| C++ -O0 | 788 ms | 45.1 MB | 123 ms | 89.4 MB | 16 KB |
| Rust -O1 | 809 ms | 45.1 MB | 147 ms | 134.0 MB | 3862 KB |
| Axle -O1 | 862 ms | 45.1 MB | 122 ms | 125.3 MB | 305 KB |
| Axle -O0 | 950 ms | 45.1 MB | 111 ms | 127.0 MB | 305 KB |
| Rust -O0 | 1.01 s | 45.1 MB | 138 ms | 131.4 MB | 3865 KB |
// Project-Euler #21 generalised — sum of amicable numbers below N.
//
// For every n in [2, N) compute d(n) = sum of proper divisors of n by
// trial division up to sqrt(n). A pair (n, m) is amicable if d(n) = m,
// d(m) = n and n != m. Every n that belongs to an amicable pair is added
// into an i64 sum, which is printed. The trial-division divisor-sum
// kernel is the hot loop: integer division / modulo throughput over N
// candidates, each one paired by a second divisor-sum call.
//
// Equivalent to euler21_amicable_sum.{c,cpp,rs} — same N, same
// trial-division method (NO sieve), same i64 sum, same printed line.
fn proper_divisor_sum(n : i64) : i64 {
let mut sum : i64 = 1;
let mut d : i64 = 2;
while (d * d <= n) {
if (n % d == 0) {
sum = sum + d;
let e : i64 = n / d;
if (e != d) {
sum = sum + e;
}
}
d = d + 1;
}
return sum;
}
fn main() : i32 {
let N : i64 = 150000;
let mut total : i64 = 0;
let mut n : i64 = 2;
while (n < N) {
let m : i64 = proper_divisor_sum(n);
if (m != n) {
let back : i64 = proper_divisor_sum(m);
if (back == n) {
total = total + n;
}
}
n = n + 1;
}
println(total);
return 0;
}Integers not expressible as the sum of two abundant numbers, at a raised 100000 limit: divisor-sum sieve, dense abundant list, then every abundant pair stamping a reachability byte array.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 146 ms | 43.9 MB | 153 ms | 96.2 MB | 16 KB |
| C++ -O2 | 147 ms | 43.9 MB | 168 ms | 97.5 MB | 16 KB |
| C++ -O3 | 152 ms | 43.9 MB | 171 ms | 97.5 MB | 16 KB |
| Axle -O3 | 219 ms | 43.9 MB | 161 ms | 127.0 MB | 306 KB |
| Axle -O2 | 226 ms | 43.9 MB | 159 ms | 125.4 MB | 306 KB |
| Rust -O2 | 343 ms | 43.9 MB | 197 ms | 147.7 MB | 3864 KB |
| Axle -O1 | 371 ms | 43.9 MB | 130 ms | 124.4 MB | 305 KB |
| Rust -O1 | 387 ms | 43.9 MB | 175 ms | 143.6 MB | 3864 KB |
| Rust -O3 | 388 ms | 43.9 MB | 196 ms | 145.0 MB | 3864 KB |
| C++ -O0 | 520 ms | 43.9 MB | 136 ms | 89.6 MB | 16 KB |
| Axle -O0 | 1.07 s | 43.9 MB | 109 ms | 125.4 MB | 306 KB |
| Rust -O0 | 2.95 s | 43.9 MB | 152 ms | 139.4 MB | 3874 KB |
// Project-Euler #23 generalised — integers not expressible as the sum of
// two abundant numbers, up to a raised limit.
//
// Three phases, all measured:
// 1. a divisor-sum sieve — every i adds itself onto its multiples 2i,
// 3i, … so divisor_sum[n] ends up as the sum of n's proper divisors;
// 2. the abundant numbers (divisor_sum[n] > n) are collected densely;
// 3. every pair (a, b) of abundants with a <= b and a + b <= limit
// stamps `reachable[a + b]`, then a linear sweep sums the indices
// never stamped.
//
// Phase 3 dominates: for limit = 100000 there are ~24700 abundants, so
// roughly 3e8 pair iterations with a scattered byte write each.
//
// The printed total is the P23 answer (4179871) for every limit at or
// above 28123 — no integer past that bound is unreachable — so the raised
// limit buys work without changing the expected line.
//
// Equivalent to euler23_non_abundant_sums.{cpp,rs} — same limit, same
// three phases, same pair order, same printed line.
fn main() : i32 {
let limit : i32 = 100000;
let mut divisor_sum : i32[100001] = i32[100001];
let mut i : i32 = 1;
while (i <= limit / 2) {
let mut j : i32 = 2 * i;
while (j <= limit) {
divisor_sum[j] = divisor_sum[j] + i;
j = j + i;
}
i = i + 1;
}
// Abundants below 100000: 24699 of them, so 26000 slots is ample.
let mut abundant : i32[26000] = i32[26000];
let mut na : i32 = 0;
let mut k : i32 = 12;
while (k <= limit) {
if (divisor_sum[k] > k) {
abundant[na] = k;
na = na + 1;
}
k = k + 1;
}
let mut reachable : bool[100001] = bool[100001];
let mut a : i32 = 0;
while (a < na) {
let mut b : i32 = a;
while (b < na) {
let sum : i32 = abundant[a] + abundant[b];
if (sum > limit) {
break;
}
reachable[sum] = true;
b = b + 1;
}
a = a + 1;
}
let mut total : i64 = 0;
let mut m : i32 = 1;
while (m <= limit) {
if (!reachable[m]) {
total = total + (m as i64);
}
m = m + 1;
}
println(total);
return 0;
}Longest recurring cycle of 1/d below 12000 via the multiplicative order of 10 — a serial dependency chain of variable-divisor modulos no compiler can strength-reduce.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 89 ms | 45.5 MB | 128 ms | 125.3 MB | 302 KB |
| Axle -O1 | 90 ms | 45.5 MB | 117 ms | 129.1 MB | 304 KB |
| Axle -O3 | 90 ms | 45.5 MB | 126 ms | 125.3 MB | 302 KB |
| C++ -O3 | 90 ms | 45.5 MB | 136 ms | 96.2 MB | 16 KB |
| Rust -O3 | 90 ms | 45.5 MB | 144 ms | 129.4 MB | 3862 KB |
| C++ -O1 | 91 ms | 45.5 MB | 135 ms | 96.0 MB | 16 KB |
| C++ -O2 | 91 ms | 45.5 MB | 137 ms | 96.1 MB | 16 KB |
| Rust -O1 | 92 ms | 45.5 MB | 146 ms | 131.6 MB | 3862 KB |
| Rust -O2 | 92 ms | 45.5 MB | 147 ms | 131.5 MB | 3862 KB |
| C++ -O0 | 103 ms | 45.5 MB | 127 ms | 89.5 MB | 16 KB |
| Axle -O0 | 111 ms | 45.5 MB | 107 ms | 125.2 MB | 305 KB |
| Rust -O0 | 115 ms | 45.5 MB | 136 ms | 127.5 MB | 3865 KB |
// Project-Euler #26 generalised — longest recurring cycle in 1/d.
//
// For every d below N the length of the repeating decimal of 1/d is the
// multiplicative order of 10 modulo d once every factor of 2 and 5 has
// been stripped (those only delay the cycle, they do not change its
// length). The order is found the direct way: iterate r = (r * 10) % d
// until r returns to 1, counting steps. That inner loop is a serial
// dependency chain of variable-divisor modulos — the divisor is `d`, not
// a constant, so no language can strength-reduce it and the cost is real
// hardware division latency, ~3.6e7 of them over the whole sweep.
//
// Two lines are printed: the d with the longest cycle, then that length.
// The comparison is a strict `>`, so the smallest such d wins on ties in
// all three languages.
//
// Equivalent to euler26_reciprocal_cycles.{cpp,rs} — same N, same
// factor-stripping, same order loop, same tie-break, same two lines.
fn cycle_length(d_orig : i32) : i32 {
let mut d : i32 = d_orig;
while (d % 2 == 0) {
d = d / 2;
}
while (d % 5 == 0) {
d = d / 5;
}
if (d == 1) {
return 0;
}
let mut r : i32 = 10 % d;
let mut k : i32 = 1;
while (r != 1) {
r = (r * 10) % d;
k = k + 1;
}
return k;
}
fn main() : i32 {
let N : i32 = 12000;
let mut best_d : i32 = 0;
let mut best_len : i32 = 0;
let mut d : i32 = 2;
while (d < N) {
let len : i32 = cycle_length(d);
if (len > best_len) {
best_len = len;
best_d = d;
}
d = d + 1;
}
println(best_d);
println(best_len);
return 0;
}Longest consecutive-prime chain over n^2 + a*n + b coefficient pairs — primality by trial division.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 220 ms | 44.5 MB | 142 ms | 96.3 MB | 16 KB |
| C++ -O3 | 221 ms | 44.5 MB | 141 ms | 96.3 MB | 16 KB |
| Axle -O3 | 246 ms | 44.5 MB | 131 ms | 127.2 MB | 305 KB |
| Axle -O2 | 249 ms | 44.5 MB | 128 ms | 125.2 MB | 305 KB |
| Rust -O3 | 255 ms | 44.5 MB | 151 ms | 130.7 MB | 3862 KB |
| Rust -O2 | 257 ms | 44.5 MB | 147 ms | 130.9 MB | 3862 KB |
| Rust -O1 | 500 ms | 44.5 MB | 147 ms | 129.6 MB | 3862 KB |
| C++ -O1 | 504 ms | 44.5 MB | 139 ms | 95.9 MB | 16 KB |
| Axle -O1 | 573 ms | 44.5 MB | 118 ms | 123.3 MB | 305 KB |
| Axle -O0 | 685 ms | 44.5 MB | 110 ms | 123.2 MB | 305 KB |
| C++ -O0 | 727 ms | 44.5 MB | 127 ms | 89.4 MB | 16 KB |
| Rust -O0 | 1.04 s | 44.5 MB | 131 ms | 130.6 MB | 3865 KB |
// Project-Euler #27 generalised — longest prime chain from n²+a·n+b.
//
// Over all coefficient pairs a in [-A, A] and b in [-B, B], find the pair
// producing the longest chain of consecutive primes for n = 0, 1, 2, …
// with the formula n² + a·n + b, testing primality by trial division.
// Track the product a·b of the best pair and the maximum chain length;
// print both. The primality test (trial division up to sqrt) inside the
// triple-nested coefficient/chain loop is the hot loop.
//
// Equivalent to euler27_quadratic_primes.{c,cpp,rs} — same A, same B,
// same iteration order, same primality test, same two printed lines
// (best product, max chain length).
// 1 if v is prime, 0 otherwise. Values <= 1 are not prime.
fn is_prime(v : i64) : i32 {
if (v < 2) {
return 0;
}
if (v < 4) {
return 1;
}
if (v % 2 == 0) {
return 0;
}
let mut d : i64 = 3;
while (d * d <= v) {
if (v % d == 0) {
return 0;
}
d = d + 2;
}
return 1;
}
fn main() : i32 {
let A : i64 = 2200;
let B : i64 = 2200;
let mut best_product : i64 = 0;
let mut max_chain : i64 = 0;
let mut a : i64 = -A;
while (a <= A) {
let mut b : i64 = -B;
while (b <= B) {
let mut n : i64 = 0;
let mut len : i64 = 0;
while (is_prime(n * n + a * n + b) == 1) {
len = len + 1;
n = n + 1;
}
if (len > max_chain) {
max_chain = len;
best_product = a * b;
}
b = b + 1;
}
a = a + 1;
}
println(best_product);
println(max_chain);
return 0;
}Coin-change ways to 5 million pence, counted mod 1e9+7 — eight unbounded-knapsack passes over a 40 MB i64 array, so sequential memory bandwidth plus one constant-divisor modulo per step.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 82 ms | 44.8 MB | 144 ms | 125.4 MB | 307 KB |
| Axle -O2 | 101 ms | 44.8 MB | 124 ms | 127.4 MB | 307 KB |
| Axle -O1 | 119 ms | 44.8 MB | 116 ms | 123.5 MB | 307 KB |
| C++ -O3 | 131 ms | 44.8 MB | 170 ms | 96.8 MB | 16 KB |
| Rust -O3 | 132 ms | 44.8 MB | 188 ms | 144.4 MB | 3864 KB |
| C++ -O2 | 133 ms | 44.8 MB | 149 ms | 96.7 MB | 16 KB |
| C++ -O1 | 139 ms | 44.8 MB | 145 ms | 95.9 MB | 16 KB |
| Rust -O2 | 153 ms | 44.8 MB | 169 ms | 144.4 MB | 3862 KB |
| Rust -O1 | 162 ms | 44.8 MB | 160 ms | 141.8 MB | 3863 KB |
| C++ -O0 | 556 ms | 44.8 MB | 135 ms | 89.8 MB | 16 KB |
| Axle -O0 | 709 ms | 44.8 MB | 114 ms | 127.1 MB | 308 KB |
| Rust -O0 | 1.17 s | 44.8 MB | 146 ms | 139.1 MB | 3870 KB |
// Project-Euler #31 generalised — coin-change ways to a very large
// target, counted modulo 1e9+7.
//
// The classic unbounded-knapsack DP: for each of the eight UK
// denominations, sweep the amount axis upward accumulating
// ways[amount] += ways[amount - coin]. With a target of 5 million that
// is 8 passes over a 40 MB i64 array — 4e7 dependent read-modify-writes
// at unit stride, so the bench reads as sequential memory bandwidth plus
// one constant-divisor modulo per step.
//
// The true count overflows every fixed-width integer long before this
// target, so each step is reduced modulo 1e9+7. The printed value is a
// fingerprint of the DP, not the mathematical number of ways — identical
// in all three languages, which is what the comparison needs.
//
// Equivalent to euler31_coin_ways_dp.{cpp,rs} — same target, same coin
// order, same modulus, same printed line.
fn main() : i32 {
let target : i32 = 5000000;
let MOD : i64 = 1000000007;
let coins : i32[8] = [1, 2, 5, 10, 20, 50, 100, 200];
let mut ways : i64[5000001] = i64[5000001];
ways[0] = 1;
let mut c : i32 = 0;
while (c < 8) {
let coin : i32 = coins[c];
let mut amount : i32 = coin;
while (amount <= target) {
ways[amount] = (ways[amount] + ways[amount - coin]) % MOD;
amount = amount + 1;
}
c = c + 1;
}
println(ways[target]);
return 0;
}Numbers equal to the sum of the factorials of their digits, scanned to 10 million with each digit's factorial recomputed by a multiply loop rather than tabulated — a nested digit loop over the whole range.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 469 ms | 44.7 MB | 134 ms | 96.0 MB | 16 KB |
| Rust -O1 | 490 ms | 44.7 MB | 147 ms | 127.6 MB | 3862 KB |
| Rust -O3 | 494 ms | 44.7 MB | 147 ms | 127.1 MB | 3863 KB |
| Rust -O2 | 502 ms | 44.7 MB | 151 ms | 130.3 MB | 3863 KB |
| Axle -O1 | 535 ms | 44.7 MB | 117 ms | 127.0 MB | 303 KB |
| Axle -O2 | 555 ms | 44.7 MB | 128 ms | 124.6 MB | 303 KB |
| Axle -O3 | 566 ms | 44.7 MB | 129 ms | 127.1 MB | 303 KB |
| C++ -O3 | 594 ms | 44.7 MB | 149 ms | 97.2 MB | 16 KB |
| C++ -O2 | 595 ms | 44.7 MB | 149 ms | 97.2 MB | 16 KB |
| Axle -O0 | 887 ms | 44.7 MB | 110 ms | 125.3 MB | 303 KB |
| C++ -O0 | 1.20 s | 44.7 MB | 123 ms | 89.4 MB | 16 KB |
| Rust -O0 | 1.62 s | 44.7 MB | 136 ms | 129.1 MB | 3865 KB |
// Project-Euler #34 generalised — numbers equal to the sum of the
// factorials of their own digits, scanned well past the mathematical
// bound.
//
// For every n below N the digits are peeled off with `% 10` / `/ 10` and
// each one's factorial is recomputed from scratch by a small multiply
// loop — deliberately not tabulated, because the recomputation is the
// kernel being measured: ~10 million iterations of a nested digit loop
// with a multiply chain inside.
//
// The scan runs to 10 million, far above the 7 * 9! = 2540160 ceiling
// where solutions can still exist, so the count and sum printed are the
// complete P34 answer (145 and 40585, summing to 40730) with the extra
// range serving only as work.
//
// Equivalent to euler34_digit_factorials.{cpp,rs} — same N, same
// recomputed factorial, same two printed lines (count, then sum).
fn factorial(n : i32) : i32 {
let mut f : i32 = 1;
let mut i : i32 = 2;
while (i <= n) {
f = f * i;
i = i + 1;
}
return f;
}
fn digit_factorial_sum(n : i32) : i32 {
let mut total : i32 = 0;
let mut x : i32 = n;
while (x > 0) {
total = total + factorial(x % 10);
x = x / 10;
}
return total;
}
fn main() : i32 {
let N : i32 = 10000000;
let mut count : i64 = 0;
let mut total : i64 = 0;
let mut n : i32 = 3;
while (n < N) {
if (digit_factorial_sum(n) == n) {
count = count + 1;
total = total + (n as i64);
}
n = n + 1;
}
println(count);
println(total);
return 0;
}Largest prime factor by trial division, summed over millions of integers — integer div/mod throughput.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Rust -O3 | 187 ms | 45.1 MB | 94 ms | 133.4 MB | 3863 KB |
| C++ -O3 | 190 ms | 45.1 MB | 95 ms | 96.1 MB | 16 KB |
| C++ -O2 | 191 ms | 45.1 MB | 94 ms | 96.1 MB | 16 KB |
| Rust -O2 | 203 ms | 45.1 MB | 94 ms | 134.1 MB | 3863 KB |
| Axle -O3 | 217 ms | 45.1 MB | 81 ms | 125.0 MB | 305 KB |
| Axle -O2 | 263 ms | 45.1 MB | 81 ms | 125.4 MB | 305 KB |
| C++ -O1 | 473 ms | 45.1 MB | 93 ms | 95.7 MB | 16 KB |
| Rust -O1 | 514 ms | 45.1 MB | 94 ms | 134.4 MB | 3863 KB |
| Axle -O1 | 518 ms | 45.1 MB | 75 ms | 122.8 MB | 305 KB |
| C++ -O0 | 566 ms | 45.1 MB | 86 ms | 89.4 MB | 16 KB |
| Axle -O0 | 577 ms | 45.1 MB | 71 ms | 123.0 MB | 305 KB |
| Rust -O0 | 625 ms | 45.1 MB | 85 ms | 130.9 MB | 3865 KB |
// Project-Euler #3 generalised — largest prime factor by trial division.
//
// For every integer n in [2, N) compute its largest prime factor and
// accumulate the running sum into an i64. The trial-division kernel
// (while d*d <= m { while m%d==0 { f=d; m/=d } d+=1 }) is the hot loop:
// integer division / modulo throughput over a few million candidates.
//
// Equivalent to euler3_largest_prime_factor.{c,cpp,rs} — same N, same
// i64 accumulation (wraparound is fine; the value is the checksum), same
// printed line.
fn largest_prime_factor(n : i64) : i64 {
let mut m : i64 = n;
let mut f : i64 = 1;
let mut d : i64 = 2;
while (d * d <= m) {
while (m % d == 0) {
f = d;
m = m / d;
}
d = d + 1;
}
if (m > 1) {
f = m;
}
return f;
}
fn main() : i32 {
let N : i64 = 500000;
let mut sum : i64 = 0;
let mut n : i64 = 2;
while (n < N) {
sum = sum + largest_prime_factor(n);
n = n + 1;
}
println(sum);
return 0;
}Palindromic products of two 4-digit numbers, palindrome-tested on all ~40.5M products with no `p > best` pre-filter — the kernel is the reverse-digits loop, not a branch-predictor lottery.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 330 ms | 44.3 MB | 84 ms | 123.3 MB | 303 KB |
| Axle -O3 | 330 ms | 44.3 MB | 85 ms | 122.4 MB | 303 KB |
| C++ -O3 | 360 ms | 44.3 MB | 96 ms | 96.2 MB | 16 KB |
| C++ -O2 | 362 ms | 44.3 MB | 95 ms | 96.2 MB | 16 KB |
| Rust -O1 | 366 ms | 44.3 MB | 94 ms | 129.3 MB | 3863 KB |
| Rust -O2 | 379 ms | 44.3 MB | 94 ms | 128.8 MB | 3862 KB |
| Rust -O3 | 380 ms | 44.3 MB | 95 ms | 128.8 MB | 3862 KB |
| C++ -O1 | 446 ms | 44.3 MB | 96 ms | 95.9 MB | 16 KB |
| Axle -O1 | 676 ms | 44.3 MB | 77 ms | 124.6 MB | 303 KB |
| Axle -O0 | 992 ms | 44.3 MB | 72 ms | 123.3 MB | 303 KB |
| C++ -O0 | 4.71 s | 44.3 MB | 89 ms | 89.5 MB | 16 KB |
| Rust -O0 | 5.22 s | 44.3 MB | 85 ms | 130.1 MB | 3865 KB |
// Project-Euler #4 generalised — palindromic products of two 4-digit
// numbers.
//
// Every pair (a, b) with LOW <= a <= b <= HIGH is multiplied and the
// product tested for being a decimal palindrome by reversing its digits.
// Unlike the original problem there is no `p > best` pre-filter: the
// palindrome test runs on all ~40.5M products, so the measured kernel is
// the reverse-digits loop (repeated division by the constant 10) rather
// than a branch-predictor lottery. The largest palindrome, how many
// pairs produce one, and the sum of those products are printed.
//
// All arithmetic is i64 so no language has to reason about the i32
// boundary at 99,980,001.
//
// Equivalent to euler4_palindrome_products.{cpp,rs} — same bounds, same
// pair order, same palindrome test on every product, same three printed
// lines.
fn reverse_digits(n : i64) : i64 {
let mut r : i64 = 0;
let mut x : i64 = n;
while (x > 0) {
r = r * 10 + x % 10;
x = x / 10;
}
return r;
}
fn main() : i32 {
let LOW : i64 = 1000;
let HIGH : i64 = 9999;
let mut best : i64 = 0;
let mut count : i64 = 0;
let mut total : i64 = 0;
let mut a : i64 = LOW;
while (a <= HIGH) {
let mut b : i64 = a;
while (b <= HIGH) {
let p : i64 = a * b;
if (p == reverse_digits(p)) {
count = count + 1;
total = total + p;
if (p > best) {
best = p;
}
}
b = b + 1;
}
a = a + 1;
}
println(best);
println(count);
println(total);
return 0;
}The 100000th prime by incremental trial division — branchy modulo-bound primality testing.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 112 ms | 45.7 MB | 94 ms | 96.2 MB | 16 KB |
| C++ -O3 | 112 ms | 45.7 MB | 94 ms | 96.2 MB | 16 KB |
| Axle -O2 | 113 ms | 45.7 MB | 80 ms | 123.2 MB | 305 KB |
| Axle -O3 | 113 ms | 45.7 MB | 80 ms | 122.7 MB | 305 KB |
| Rust -O2 | 113 ms | 45.7 MB | 92 ms | 130.5 MB | 3862 KB |
| Rust -O3 | 113 ms | 45.7 MB | 92 ms | 130.7 MB | 3862 KB |
| C++ -O1 | 312 ms | 45.7 MB | 94 ms | 95.9 MB | 16 KB |
| Rust -O1 | 333 ms | 45.7 MB | 91 ms | 132.5 MB | 3862 KB |
| C++ -O0 | 375 ms | 45.7 MB | 85 ms | 89.4 MB | 16 KB |
| Axle -O1 | 379 ms | 45.7 MB | 75 ms | 122.5 MB | 305 KB |
| Axle -O0 | 407 ms | 45.7 MB | 71 ms | 124.9 MB | 305 KB |
| Rust -O0 | 434 ms | 45.7 MB | 86 ms | 128.6 MB | 3864 KB |
// Project-Euler #7 generalised — the K-th prime by trial division.
//
// Counts primes from 2 upward, testing each candidate by dividing it by
// every d with d*d <= cand. No sieve, no precomputed table: the work is
// the same modulo-bound primality test repeated until the K-th prime is
// reached. The hot loop is integer modulo + the d*d bound check.
//
// Equivalent to euler7_nth_prime.{c,cpp,rs} — same K, same algorithm,
// same printed prime.
fn is_prime(n : i64) : i32 {
if (n < 2) {
return 0;
}
if (n % 2 == 0) {
if (n == 2) {
return 1;
}
return 0;
}
let mut d : i64 = 3;
while (d * d <= n) {
if (n % d == 0) {
return 0;
}
d = d + 2;
}
return 1;
}
fn main() : i32 {
let K : i64 = 100000;
let mut count : i64 = 0;
let mut cand : i64 = 1;
let mut prime : i64 = 2;
while (count < K) {
cand = cand + 1;
if (is_prime(cand) == 1) {
count = count + 1;
prime = cand;
}
}
println(prime);
return 0;
}Brute-force O(N^2) Pythagorean-triplet search with a shared integer-sqrt verification.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 411 ms | 47.0 MB | 82 ms | 126.4 MB | 305 KB |
| Axle -O3 | 411 ms | 47.0 MB | 83 ms | 123.4 MB | 305 KB |
| C++ -O2 | 441 ms | 47.0 MB | 94 ms | 96.3 MB | 16 KB |
| C++ -O3 | 441 ms | 47.0 MB | 94 ms | 96.3 MB | 16 KB |
| Rust -O3 | 448 ms | 47.0 MB | 94 ms | 134.5 MB | 3862 KB |
| Rust -O2 | 449 ms | 47.0 MB | 94 ms | 132.3 MB | 3862 KB |
| Axle -O1 | 607 ms | 47.0 MB | 76 ms | 123.1 MB | 305 KB |
| Rust -O1 | 743 ms | 47.0 MB | 94 ms | 132.6 MB | 3862 KB |
| C++ -O1 | 749 ms | 47.0 MB | 92 ms | 95.8 MB | 16 KB |
| Axle -O0 | 941 ms | 47.0 MB | 71 ms | 121.5 MB | 305 KB |
| C++ -O0 | 1.64 s | 47.0 MB | 87 ms | 89.3 MB | 16 KB |
| Rust -O0 | 1.78 s | 47.0 MB | 85 ms | 133.7 MB | 3865 KB |
// Project-Euler #9 generalised — Pythagorean triplets by brute force.
//
// Counts every triplet (a, b, c) with a < b < c, a*a + b*b == c*c and
// a + b + c < N, accumulating the checksum sum-of-(a*b*c) into an i64.
// The structure is a genuine O(N^2) double loop (a in [1, N), b in
// [a+1, N)); `c` is recovered with an integer square root and verified
// with c*c == a*a + b*b, so the kernel stays compute-bound (multiplies +
// the isqrt refine loop) rather than memory-bound.
//
// `isqrt` is implemented identically across all four languages (no libm)
// so the triplet set — and therefore the checksum — is byte-identical.
//
// Equivalent to euler9_pythagorean_triplets.{c,cpp,rs} — same N, same
// nested-loop structure, same i64 accumulation, same printed line.
// Floor of the square root of a non-negative i64, by Newton refinement.
fn isqrt(n : i64) : i64 {
if (n < 2) {
return n;
}
let mut x : i64 = n;
let mut y : i64 = (x + 1) / 2;
while (y < x) {
x = y;
y = (x + n / x) / 2;
}
return x;
}
fn main() : i32 {
let N : i64 = 3200;
let mut checksum : i64 = 0;
let mut a : i64 = 1;
while (a < N) {
let mut b : i64 = a + 1;
while (b < N) {
let c2 : i64 = a * a + b * b;
let c : i64 = isqrt(c2);
if (c * c == c2 && b < c && a + b + c < N) {
checksum = checksum + a * b * c;
}
b = b + 1;
}
a = a + 1;
}
println(checksum);
return 0;
}