Skip to content

Repository files navigation

Flow x Project Euler

Verify (C backend)MLIR backend

Solving Project Euler in Flow, a statically typed language that reads close to high-level math and compiles to native C and MLIR.

1007 problems solved. All 1007 known answers in answers.txt have Flow solutions. 262 use native C/C++ helpers for algorithms that need __int128, GMP, MPFR, or NTT.

For the conventions learned from this corpus, see FLOW_IDIOMS.md. The rule is simple: adopt an idiom when repeated real solutions show that it improves intent or reuse without changing the low-level execution contract.

Quick start

git clone https://github.com/flooooooooooow/flow.git ~/flow
export FLOW_REPO=$HOME/flow
./scripts/run.sh 1 # run problem 001
./scripts/run.sh all # run every solved problem
./scripts/verify.sh # check all against answers.txt
./scripts/bench.sh # Flow-generated C vs hand-written C

Each problems/pNNN.flow prints a single answer line.

Point FLOW_BIN at $FLOW_REPO/flow (not a symlink). Symlinked ~/.local/bin/flow breaks Flow's SCRIPT_DIR resolution.

MLIR backend

Flow also compiles through MLIR. This repo tests both paths in CI.

brew install llvm # provides mlir-opt, mlir-translate, llcexport LLVM_BIN=/opt/homebrew/opt/llvm/bin
./scripts/verify-mlir.sh # verify through MLIR backend
./scripts/verify-mlir.sh 1 50 # problems 1-50 only

See docs/backends.md for details on the MLIR pipeline, current limitations, and manual compilation steps.

Progress

RangeSolvedStatus
001-200200done
201-400200done
401-600200done
601-1007407in progress

1007 known answers in answers.txt. All 1007 have Flow solutions.

262 problems use native C/C++ helpers for NTT, __int128, GMP, or MPFR.

Why Flow here

ConcernFlowHand-written CPython
Expressivenessranges, typed helpers, clear control flowverbose loopsexcellent, but slow
Performancecompiles to -O3 C / MLIRbaseline10-100x slower on tight loops
Typesi32 / i64 where it matterssameduck typing
Deploymentone native binarysameinterpreter + deps

Benchmark (PE 010 sieve, 2e6)

Same algorithm, both compiled with clang -O3 -march=native, 200 in-process rounds on Apple Silicon.

Implus / run
Flow to C1675
Hand-written C1767

Reproduce with ./scripts/bench.sh.

Layout

problems/ one .flow file per Euler problem
problems/native/ C/C++ helpers (NTT, i128, GMP, MPFR)
lib/ shared zero-cost Flow kernels and representations
data/ input grids, digit strings, tables
bench/ hand-written C twins + timing harness
scripts/ run, verify, verify-mlir, bench
answers.txt expected answers for automated checks
FLOW_IDIOMS.md corpus-derived Flow conventions and adoption rules
docs/ architecture, backends, adding solutions

Example: PE 001

function sum_multiples(limit: i64, step: i64) -> i64 {
let mut total: i64 = 0
for n in 0..limit step step {
total = total + n
}
return total
}
function solve(limit: i64) -> i64 {
return sum_multiples(limit, 3) + sum_multiples(limit, 5) - sum_multiples(limit, 15)
}

Readable arithmetic-series code. Flow lowers the stepped for to a tight C loop.

Example: native helper (PE 775)

Some algorithms need __int128 or modular arithmetic that is cleaner in C. The Flow file declares an extern and calls it:

extern {
function p775_native() -> i64
}
function main() -> i32 {
printf("%lld\n", p775_native())
return 0
}

The C file under problems/native/p775.c implements the solver and exports long long p775_native(void). The build system auto-links GMP and MPFR if Homebrew headers are present.

See docs/adding-solutions.md for the full guide.

CI

Three GitHub Actions workflows run on every push and pull request:

  • verify.yml: compiles every solution through the C backend and checks answers against answers.txt. Problems listed in .ci-skip.txt are skipped (too slow, crash, or broken). Runs on macos-14 with a 120-minute timeout.
  • mlir.yml: compiles every pure-Flow solution through the MLIR backend (Flow -> MLIR -> LLVM IR -> llc -> clang). Reports MLIR lowering failures separately from answer mismatches. Runs on macos-14 with a 60-minute timeout.
  • site.yml: generates a static GitHub Pages site showing the Flow source, generated C, generated MLIR, and output for every problem. Deploys to GitHub Pages on push to main.

Documentation

License

Solutions are educational. Project Euler problems remain (c) Project Euler.

About

Project Euler solutions in Flow — expressive syntax, C-speed binaries

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages