aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Math.bruijn
blob: db97d3c0947d6baf12e85ae04f3ff8b28570ca6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# MIT License, Copyright (c) 2022 Marvin Borner

:import std/List .

:input std/Number .

# greatest common divisor
gcd z [[[(1 =? 0) case-eq ((1 >? 0) case-gre case-les)]]]
	case-eq 1
	case-gre 2 (1 - 0) 0
	case-les 2 1 (0 - 1)

:test ((gcd (+2) (+4)) =? ((+2))) (true)
:test ((gcd (+10) (+5)) =? ((+5))) (true)
:test ((gcd (+3) (+8)) =? ((+1))) (true)

# power function
pow [(!!) (iterate ((*) 0) (+1))]

…**… pow

:test (((+2) ** (+3)) =? ((+8))) (true)

# factorial function
# fac Z [[(0 <? (+2)) (+1) (0 * (1 --0))]]
fac [Π (take 0 (iterate ++‣ (+1)))]

:test ((fac (+3)) =? (+6)) (true)

# fibonacci sequence
# fibs Z [(+1) : ((+1) : (zip-with (+) 0 ~0))]
fibs fst <$> (iterate [~0 : (^0 + ~0)] ((+0) : (+1)))

fib [fibs !! ++0]

:test (fib (+5)) ((+8))