Benchmarks

Euler benchmarks

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

Euler

euler10_sum_primes_sieve

Sieve of Eratosthenes below 27M — strided composite marking plus a linear prime-sum sweep.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O3261 ms106.0 MB105 ms97.1 MB16 KB
Axle -O3262 ms105.3 MB87 ms123.4 MB308 KB
Rust -O2262 ms104.7 MB113 ms144.5 MB3863 KB
Axle -O2263 ms105.3 MB85 ms123.0 MB308 KB
C++ -O2264 ms105.9 MB107 ms97.2 MB16 KB
Rust -O3264 ms104.6 MB113 ms145.5 MB3863 KB
Axle -O1271 ms105.3 MB75 ms123.5 MB307 KB
C++ -O1271 ms106.0 MB99 ms95.9 MB16 KB
Rust -O1280 ms104.7 MB105 ms141.3 MB3863 KB
Axle -O0361 ms105.3 MB69 ms123.1 MB307 KB
C++ -O0372 ms106.0 MB91 ms89.6 MB16 KB
Rust -O0551 ms104.7 MB92 ms138.1 MB3870 KB
▸ Source
// 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;
}

euler12_divisor_counts

Divisor counts of the first 13000 triangular numbers by trial division — integer div/mod throughput.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O2132 ms45.5 MB94 ms96.2 MB16 KB
C++ -O3132 ms45.5 MB93 ms96.2 MB16 KB
Axle -O2161 ms45.5 MB80 ms123.3 MB305 KB
Axle -O3161 ms45.5 MB81 ms123.5 MB305 KB
Rust -O3161 ms45.5 MB93 ms134.8 MB3862 KB
Rust -O2162 ms45.5 MB94 ms135.7 MB3862 KB
C++ -O1361 ms45.5 MB92 ms95.9 MB16 KB
Rust -O1395 ms45.5 MB93 ms134.3 MB3862 KB
C++ -O0413 ms45.5 MB88 ms89.4 MB16 KB
Axle -O1417 ms45.5 MB75 ms123.3 MB305 KB
Axle -O0481 ms45.5 MB70 ms123.0 MB305 KB
Rust -O0523 ms45.5 MB87 ms133.2 MB3865 KB
▸ Source
// 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;
}

euler14_longest_collatz

Longest Collatz chain under 2e6, computed without memoization (compute-bound).

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O1221 ms43.8 MB75 ms124.6 MB303 KB
Axle -O2232 ms43.8 MB79 ms124.9 MB303 KB
Axle -O3232 ms43.8 MB80 ms123.3 MB303 KB
Rust -O2232 ms43.8 MB91 ms129.3 MB3862 KB
Rust -O1234 ms43.8 MB91 ms131.6 MB3862 KB
Rust -O3235 ms43.8 MB91 ms131.0 MB3862 KB
C++ -O2282 ms43.8 MB93 ms96.0 MB16 KB
C++ -O3282 ms43.8 MB94 ms96.2 MB16 KB
C++ -O1506 ms43.8 MB91 ms95.8 MB16 KB
Axle -O0972 ms43.8 MB70 ms125.0 MB303 KB
Rust -O04.34 s43.8 MB86 ms131.0 MB3865 KB
C++ -O04.43 s43.8 MB87 ms89.4 MB16 KB
▸ Source
// 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;
}

euler1_multiples_sum

Sum of all multiples of 3 or 5 below 2.5e8 via an explicit modulo scan (no closed-form shortcut).

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O3113 ms45.7 MB94 ms96.5 MB16 KB
C++ -O2114 ms45.7 MB96 ms96.7 MB16 KB
Axle -O3115 ms45.7 MB78 ms125.4 MB303 KB
Rust -O3116 ms45.7 MB91 ms133.9 MB3862 KB
Rust -O2117 ms45.7 MB92 ms134.5 MB3862 KB
Axle -O2119 ms45.7 MB78 ms123.3 MB303 KB
Rust -O1257 ms45.7 MB90 ms133.5 MB3862 KB
C++ -O1489 ms45.7 MB91 ms95.4 MB16 KB
Axle -O1704 ms45.7 MB73 ms125.3 MB303 KB
Axle -O01.12 s45.7 MB70 ms123.1 MB303 KB
C++ -O04.64 s45.7 MB87 ms89.0 MB16 KB
Rust -O05.37 s45.7 MB86 ms131.4 MB3864 KB
▸ Source
// 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;
}

euler20_factorial_digit_sum

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O2399 ms45.4 MB156 ms97.2 MB16 KB
C++ -O3401 ms45.4 MB159 ms97.2 MB16 KB
C++ -O1404 ms45.4 MB150 ms96.1 MB16 KB
Rust -O2424 ms45.4 MB174 ms144.5 MB3863 KB
Rust -O1430 ms45.4 MB157 ms142.9 MB3863 KB
Rust -O3431 ms45.4 MB173 ms144.5 MB3863 KB
Axle -O2463 ms45.4 MB145 ms125.4 MB305 KB
Axle -O3463 ms45.4 MB147 ms126.7 MB305 KB
Axle -O1494 ms45.4 MB126 ms125.2 MB305 KB
Axle -O01.23 s45.4 MB111 ms128.7 MB305 KB
C++ -O01.70 s45.4 MB137 ms89.6 MB16 KB
Rust -O03.67 s45.4 MB150 ms141.6 MB3871 KB
▸ Source
// 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;
}

euler21_amicable_sum

Sum of amicable numbers below 150K via paired proper-divisor-sum trial division.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O3323 ms45.1 MB142 ms96.2 MB16 KB
C++ -O2329 ms45.1 MB139 ms96.2 MB16 KB
Axle -O3347 ms45.1 MB128 ms127.3 MB305 KB
Axle -O2371 ms45.1 MB128 ms125.2 MB305 KB
Rust -O2405 ms45.1 MB149 ms135.1 MB3862 KB
Rust -O3414 ms45.1 MB146 ms133.4 MB3862 KB
C++ -O1695 ms45.1 MB135 ms95.8 MB16 KB
C++ -O0788 ms45.1 MB123 ms89.4 MB16 KB
Rust -O1809 ms45.1 MB147 ms134.0 MB3862 KB
Axle -O1862 ms45.1 MB122 ms125.3 MB305 KB
Axle -O0950 ms45.1 MB111 ms127.0 MB305 KB
Rust -O01.01 s45.1 MB138 ms131.4 MB3865 KB
▸ Source
// 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;
}

euler23_non_abundant_sums

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O1146 ms43.9 MB153 ms96.2 MB16 KB
C++ -O2147 ms43.9 MB168 ms97.5 MB16 KB
C++ -O3152 ms43.9 MB171 ms97.5 MB16 KB
Axle -O3219 ms43.9 MB161 ms127.0 MB306 KB
Axle -O2226 ms43.9 MB159 ms125.4 MB306 KB
Rust -O2343 ms43.9 MB197 ms147.7 MB3864 KB
Axle -O1371 ms43.9 MB130 ms124.4 MB305 KB
Rust -O1387 ms43.9 MB175 ms143.6 MB3864 KB
Rust -O3388 ms43.9 MB196 ms145.0 MB3864 KB
C++ -O0520 ms43.9 MB136 ms89.6 MB16 KB
Axle -O01.07 s43.9 MB109 ms125.4 MB306 KB
Rust -O02.95 s43.9 MB152 ms139.4 MB3874 KB
▸ Source
// 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;
}

euler26_reciprocal_cycles

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O289 ms45.5 MB128 ms125.3 MB302 KB
Axle -O190 ms45.5 MB117 ms129.1 MB304 KB
Axle -O390 ms45.5 MB126 ms125.3 MB302 KB
C++ -O390 ms45.5 MB136 ms96.2 MB16 KB
Rust -O390 ms45.5 MB144 ms129.4 MB3862 KB
C++ -O191 ms45.5 MB135 ms96.0 MB16 KB
C++ -O291 ms45.5 MB137 ms96.1 MB16 KB
Rust -O192 ms45.5 MB146 ms131.6 MB3862 KB
Rust -O292 ms45.5 MB147 ms131.5 MB3862 KB
C++ -O0103 ms45.5 MB127 ms89.5 MB16 KB
Axle -O0111 ms45.5 MB107 ms125.2 MB305 KB
Rust -O0115 ms45.5 MB136 ms127.5 MB3865 KB
▸ Source
// 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;
}

euler27_quadratic_primes

Longest consecutive-prime chain over n^2 + a*n + b coefficient pairs — primality by trial division.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O2220 ms44.5 MB142 ms96.3 MB16 KB
C++ -O3221 ms44.5 MB141 ms96.3 MB16 KB
Axle -O3246 ms44.5 MB131 ms127.2 MB305 KB
Axle -O2249 ms44.5 MB128 ms125.2 MB305 KB
Rust -O3255 ms44.5 MB151 ms130.7 MB3862 KB
Rust -O2257 ms44.5 MB147 ms130.9 MB3862 KB
Rust -O1500 ms44.5 MB147 ms129.6 MB3862 KB
C++ -O1504 ms44.5 MB139 ms95.9 MB16 KB
Axle -O1573 ms44.5 MB118 ms123.3 MB305 KB
Axle -O0685 ms44.5 MB110 ms123.2 MB305 KB
C++ -O0727 ms44.5 MB127 ms89.4 MB16 KB
Rust -O01.04 s44.5 MB131 ms130.6 MB3865 KB
▸ Source
// 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;
}

euler31_coin_ways_dp

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O382 ms44.8 MB144 ms125.4 MB307 KB
Axle -O2101 ms44.8 MB124 ms127.4 MB307 KB
Axle -O1119 ms44.8 MB116 ms123.5 MB307 KB
C++ -O3131 ms44.8 MB170 ms96.8 MB16 KB
Rust -O3132 ms44.8 MB188 ms144.4 MB3864 KB
C++ -O2133 ms44.8 MB149 ms96.7 MB16 KB
C++ -O1139 ms44.8 MB145 ms95.9 MB16 KB
Rust -O2153 ms44.8 MB169 ms144.4 MB3862 KB
Rust -O1162 ms44.8 MB160 ms141.8 MB3863 KB
C++ -O0556 ms44.8 MB135 ms89.8 MB16 KB
Axle -O0709 ms44.8 MB114 ms127.1 MB308 KB
Rust -O01.17 s44.8 MB146 ms139.1 MB3870 KB
▸ Source
// 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;
}

euler34_digit_factorials

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O1469 ms44.7 MB134 ms96.0 MB16 KB
Rust -O1490 ms44.7 MB147 ms127.6 MB3862 KB
Rust -O3494 ms44.7 MB147 ms127.1 MB3863 KB
Rust -O2502 ms44.7 MB151 ms130.3 MB3863 KB
Axle -O1535 ms44.7 MB117 ms127.0 MB303 KB
Axle -O2555 ms44.7 MB128 ms124.6 MB303 KB
Axle -O3566 ms44.7 MB129 ms127.1 MB303 KB
C++ -O3594 ms44.7 MB149 ms97.2 MB16 KB
C++ -O2595 ms44.7 MB149 ms97.2 MB16 KB
Axle -O0887 ms44.7 MB110 ms125.3 MB303 KB
C++ -O01.20 s44.7 MB123 ms89.4 MB16 KB
Rust -O01.62 s44.7 MB136 ms129.1 MB3865 KB
▸ Source
// 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;
}

euler3_largest_prime_factor

Largest prime factor by trial division, summed over millions of integers — integer div/mod throughput.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Rust -O3187 ms45.1 MB94 ms133.4 MB3863 KB
C++ -O3190 ms45.1 MB95 ms96.1 MB16 KB
C++ -O2191 ms45.1 MB94 ms96.1 MB16 KB
Rust -O2203 ms45.1 MB94 ms134.1 MB3863 KB
Axle -O3217 ms45.1 MB81 ms125.0 MB305 KB
Axle -O2263 ms45.1 MB81 ms125.4 MB305 KB
C++ -O1473 ms45.1 MB93 ms95.7 MB16 KB
Rust -O1514 ms45.1 MB94 ms134.4 MB3863 KB
Axle -O1518 ms45.1 MB75 ms122.8 MB305 KB
C++ -O0566 ms45.1 MB86 ms89.4 MB16 KB
Axle -O0577 ms45.1 MB71 ms123.0 MB305 KB
Rust -O0625 ms45.1 MB85 ms130.9 MB3865 KB
▸ Source
// 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;
}

euler4_palindrome_products

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.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2330 ms44.3 MB84 ms123.3 MB303 KB
Axle -O3330 ms44.3 MB85 ms122.4 MB303 KB
C++ -O3360 ms44.3 MB96 ms96.2 MB16 KB
C++ -O2362 ms44.3 MB95 ms96.2 MB16 KB
Rust -O1366 ms44.3 MB94 ms129.3 MB3863 KB
Rust -O2379 ms44.3 MB94 ms128.8 MB3862 KB
Rust -O3380 ms44.3 MB95 ms128.8 MB3862 KB
C++ -O1446 ms44.3 MB96 ms95.9 MB16 KB
Axle -O1676 ms44.3 MB77 ms124.6 MB303 KB
Axle -O0992 ms44.3 MB72 ms123.3 MB303 KB
C++ -O04.71 s44.3 MB89 ms89.5 MB16 KB
Rust -O05.22 s44.3 MB85 ms130.1 MB3865 KB
▸ Source
// 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;
}

euler7_nth_prime

The 100000th prime by incremental trial division — branchy modulo-bound primality testing.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O2112 ms45.7 MB94 ms96.2 MB16 KB
C++ -O3112 ms45.7 MB94 ms96.2 MB16 KB
Axle -O2113 ms45.7 MB80 ms123.2 MB305 KB
Axle -O3113 ms45.7 MB80 ms122.7 MB305 KB
Rust -O2113 ms45.7 MB92 ms130.5 MB3862 KB
Rust -O3113 ms45.7 MB92 ms130.7 MB3862 KB
C++ -O1312 ms45.7 MB94 ms95.9 MB16 KB
Rust -O1333 ms45.7 MB91 ms132.5 MB3862 KB
C++ -O0375 ms45.7 MB85 ms89.4 MB16 KB
Axle -O1379 ms45.7 MB75 ms122.5 MB305 KB
Axle -O0407 ms45.7 MB71 ms124.9 MB305 KB
Rust -O0434 ms45.7 MB86 ms128.6 MB3864 KB
▸ Source
// 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;
}

euler9_pythagorean_triplets

Brute-force O(N^2) Pythagorean-triplet search with a shared integer-sqrt verification.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O2411 ms47.0 MB82 ms126.4 MB305 KB
Axle -O3411 ms47.0 MB83 ms123.4 MB305 KB
C++ -O2441 ms47.0 MB94 ms96.3 MB16 KB
C++ -O3441 ms47.0 MB94 ms96.3 MB16 KB
Rust -O3448 ms47.0 MB94 ms134.5 MB3862 KB
Rust -O2449 ms47.0 MB94 ms132.3 MB3862 KB
Axle -O1607 ms47.0 MB76 ms123.1 MB305 KB
Rust -O1743 ms47.0 MB94 ms132.6 MB3862 KB
C++ -O1749 ms47.0 MB92 ms95.8 MB16 KB
Axle -O0941 ms47.0 MB71 ms121.5 MB305 KB
C++ -O01.64 s47.0 MB87 ms89.3 MB16 KB
Rust -O01.78 s47.0 MB85 ms133.7 MB3865 KB
▸ Source
// 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;
}