tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

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

52 lignes · 933 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.
4package main
5
6import "fmt"
7
8type tree struct {
9 leaf bool
10 value int64
11 left *tree
12 right *tree
13}
14
15func makeTree(d, v int64) *tree {
16 if d == 0 {
17 return &tree{leaf: true, value: v}
18 }
19 return &tree{left: makeTree(d-1, v), right: makeTree(d-1, v)}
20}
21
22func check(t *tree) int64 {
23 if t.leaf {
24 return t.value % 2
25 }
26 return 1 + check(t.left) + check(t.right)
27}
28
29func pow2(k int64) int64 {
30 if k == 0 {
31 return 1
32 }
33 return 2 * pow2(k-1)
34}
35
36func repeat(i, d, acc int64) int64 {
37 if i == 0 {
38 return acc
39 }
40 return repeat(i-1, d, acc+check(makeTree(d, i)))
41}
42
43func depths(d, max, acc int64) int64 {
44 if d > max {
45 return acc
46 }
47 return depths(d+2, max, acc+repeat(pow2(max-d+4), d, 0))
48}
49
50func main() {
51 fmt.Println(depths(4, 16, 0))
52}