|
1 |
|
2 def zero(x) = 0; |
|
3 |
|
4 def suc(x) = x + 1; |
|
5 |
|
6 def pred(x) = |
|
7 if x == 0 then x else x - 1; |
|
8 |
|
9 def add(x, y) = |
|
10 if x == 0 then y else suc(add(x - 1, y)); |
|
11 |
|
12 def mult(x, y) = |
|
13 if x == 0 then 0 else add(y, mult(x - 1, y)); |
|
14 |
|
15 def pow(x, y) = |
|
16 if y == 0 then 1 else mult(x, pow(x, y - 1)); |
|
17 |
|
18 def fib(n) = if n == 0 then 0 |
|
19 else if n == 1 then 1 |
|
20 else fib(n - 1) + fib(n - 2); |
|
21 |
|
22 def fact(n) = |
|
23 if n == 0 then 1 else n * fact(n - 1); |
|
24 |
|
25 def ack(m, n) = if m == 0 then n + 1 |
|
26 else if n == 0 then ack(m - 1, 1) |
|
27 else ack(m - 1, ack(m, n - 1)); |
|
28 |
|
29 def stack_test(x) = x + 1 + 2 + 3 + 4 + 5; |
|
30 |
|
31 def div(x, y) = x / y; //integer division |
|
32 |
|
33 def rem(x, y) = x % y; //remainder |
|
34 |
|
35 def gcd(a, b) = |
|
36 if b == 0 then a else gcd(b, a % b); |
|
37 |
|
38 def is_prime_aux(n, i) = |
|
39 if n % i == 0 then 0 |
|
40 else if (i * i) <= n then is_prime_aux(n, i + 1) |
|
41 else 1; |
|
42 |
|
43 def is_prime(n) = if n == 2 then 1 else is_prime_aux(n, 2); |
|
44 |
|
45 def primes(n) = |
|
46 if n == 0 then 0 |
|
47 else if is_prime(n) == 1 then (write n; primes(n - 1)) |
|
48 else primes(n - 1); |
|
49 |
|
50 def is_collatz(n) = |
|
51 if n == 1 then 1 |
|
52 else if n % 2 == 0 then is_collatz(n / 2) |
|
53 else is_collatz(3 * n + 1); |
|
54 |
|
55 def collatz_aux(n, i) = |
|
56 if i > n then 0 |
|
57 else if is_collatz(i) == 1 then (write i; collatz_aux(n, i + 1)) |
|
58 else collatz_aux(n, i + 1); |
|
59 |
|
60 def collatz(n) = collatz_aux(n, 1); |
|
61 |
|
62 def facT(n, acc) = |
|
63 if n == 0 then acc else facT(n - 1, n * acc); |
|
64 |
|
65 |
|
66 //zero(3) |
|
67 //suc(8) |
|
68 //pred(7) |
|
69 //write(add(3, 4)) |
|
70 //mult(4,5) |
|
71 //pow(2, 3) |
|
72 //fib(20) |
|
73 //(write(fact(5)) ; fact(6)) |
|
74 //(write(1) ; 2) |
|
75 //write(ack(3, 12)) // for tail-rec test |
|
76 //stack_test(0) |
|
77 //(write (div(11, 3)); rem(11, 3)) |
|
78 //gcd(54, 24) |
|
79 //is_prime(2) |
|
80 primes(1000) |
|
81 //primes(1000000) |
|
82 //collatz(4000) |
|
83 //collatz(5000) |
|
84 //facT(6, 1) |
|
85 |