
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | // A map/filter/fold pipeline over a million integers, ten times (each |
| 2 | // round a little longer, so that no compiler computes it only once). |
| 3 | import gleam/int |
| 4 | import gleam/io |
| 5 | import gleam/list |
| 6 | |
| 7 | // `gleam/list` has no `range`: build `[from, …, to]` (tail-recursive). |
| 8 | fn range(from: Int, to: Int, acc: List(Int)) -> List(Int) { |
| 9 | case from > to { |
| 10 | True -> acc |
| 11 | False -> range(from, to - 1, [to, ..acc]) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | fn round(k: Int, acc: Int) -> Int { |
| 16 | case k == 0 { |
| 17 | True -> acc |
| 18 | False -> { |
| 19 | let doubled = list.map(range(1, 1_000_000 + k, []), fn(x) { x * 2 }) |
| 20 | let kept = list.filter(doubled, fn(x) { int.remainder(x, 3) == Ok(0) }) |
| 21 | let sum = list.fold(kept, 0, fn(a, x) { a + x }) |
| 22 | round(k - 1, acc + sum) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | pub fn main() { |
| 28 | io.println(int.to_string(round(10, 0))) |
| 29 | } |