tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

52 lignes · 1017 octetsBrut · Historique
1// Allocation: build and walk many binary trees (after the Computer
2// Language Benchmarks Game's binary-trees). Each tree holds its round's
3// number, so that no compiler builds it only once.
4import gleam/int
5import gleam/io
6
7type Tree {
8 Leaf(Int)
9 Node(Tree, Tree)
10}
11
12fn make(d: Int, v: Int) -> Tree {
13 case d == 0 {
14 True -> Leaf(v)
15 False -> Node(make(d - 1, v), make(d - 1, v))
16 }
17}
18
19fn check(t: Tree) -> Int {
20 case t {
21 Leaf(v) -> case int.remainder(v, 2) {
22 Ok(r) -> r
23 _ -> 0
24 }
25 Node(l, r) -> 1 + check(l) + check(r)
26 }
27}
28
29fn pow2(k: Int) -> Int {
30 case k == 0 {
31 True -> 1
32 False -> 2 * pow2(k - 1)
33 }
34}
35
36fn repeat(i: Int, d: Int, acc: Int) -> Int {
37 case i == 0 {
38 True -> acc
39 False -> repeat(i - 1, d, acc + check(make(d, i)))
40 }
41}
42
43fn depths(d: Int, max: Int, acc: Int) -> Int {
44 case d > max {
45 True -> acc
46 False -> depths(d + 2, max, acc + repeat(pow2(max - d + 4), d, 0))
47 }
48}
49
50pub fn main() {
51 io.println(int.to_string(depths(4, 16, 0)))
52}