tobogganSe connecter

louise

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

53 lignes · 854 octetsBrut · Historique
1// A persistent ordered map: insert, look up, remove.
2
3import std/dict.{type Dict}
4import std/io
5import std/list
6import std/option
7
8fn fill :: d: Dict<Int, Int> i: Int n: Int -> Dict<Int, Int>
9{
10 if i > n
11 {
12 d
13 }
14 else
15 {
16 fill (dict.insert d (i * 7919 % n) i) (i + 1) n
17 }
18}
19
20fn probe :: d: Dict<Int, Int> i: Int n: Int acc: Int -> Int
21{
22 if i > n
23 {
24 acc
25 }
26 else
27 {
28 let found = match dict.get d i { Some(v) -> v % 10, None -> 0 }
29 probe d (i + 1) n (acc + found)
30 }
31}
32
33fn drain :: d: Dict<Int, Int> i: Int n: Int -> Dict<Int, Int>
34{
35 if i > n
36 {
37 d
38 }
39 else
40 {
41 drain (dict.remove d i) (i + 2) n
42 }
43}
44
45pub fn main :: -> () / <io>
46{
47 let n = 100000
48 let d = fill (dict.empty ()) 1 n
49 let sum = probe d 0 n 0
50 let left = drain d 0 n
51 io.println (show sum <> " " <> show (dict.size left))
52}
53//> 450000 50000