// A map/filter/fold pipeline over a million integers, ten times (each // round a little longer, so that no compiler computes it only once). import gleam/int import gleam/io import gleam/list // `gleam/list` has no `range`: build `[from, …, to]` (tail-recursive). fn range(from: Int, to: Int, acc: List(Int)) -> List(Int) { case from > to { True -> acc False -> range(from, to - 1, [to, ..acc]) } } fn round(k: Int, acc: Int) -> Int { case k == 0 { True -> acc False -> { let doubled = list.map(range(1, 1_000_000 + k, []), fn(x) { x * 2 }) let kept = list.filter(doubled, fn(x) { int.remainder(x, 3) == Ok(0) }) let sum = list.fold(kept, 0, fn(a, x) { a + x }) round(k - 1, acc + sum) } } } pub fn main() { io.println(int.to_string(round(10, 0))) }