author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 18 Sep 2020 00:33:03 +0100 | |
changeset 757 | ea0be0662be0 |
parent 742 | b5b5583a3a08 |
permissions | -rw-r--r-- |
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
def zero(x) = 0; |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
def suc(x) = x + 1; |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
656 | 6 |
def pred(x) = |
7 |
if x == 0 then x else x - 1; |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
656 | 9 |
def add(x, y) = |
10 |
if x == 0 then y else suc(add(x - 1, y)); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
656 | 12 |
def mult(x, y) = |
13 |
if x == 0 then 0 else add(y, mult(x - 1, y)); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
656 | 15 |
def pow(x, y) = |
16 |
if y == 0 then 1 else mult(x, pow(x, y - 1)); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
def fib(n) = if n == 0 then 0 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
else if n == 1 then 1 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
else fib(n - 1) + fib(n - 2); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
656 | 22 |
def fact(n) = |
23 |
if n == 0 then 1 else n * fact(n - 1); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
def ack(m, n) = if m == 0 then n + 1 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
else if n == 0 then ack(m - 1, 1) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
else ack(m - 1, ack(m, n - 1)); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
def stack_test(x) = x + 1 + 2 + 3 + 4 + 5; |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
def div(x, y) = x / y; //integer division |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
def rem(x, y) = x % y; //remainder |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
656 | 35 |
def gcd(a, b) = |
36 |
if b == 0 then a else gcd(b, a % b); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
def is_prime_aux(n, i) = |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
if n % i == 0 then 0 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
else if (i * i) <= n then is_prime_aux(n, i + 1) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
else 1; |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def is_prime(n) = if n == 2 then 1 else is_prime_aux(n, 2); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
def primes(n) = |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
if n == 0 then 0 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
else if is_prime(n) == 1 then (write n; primes(n - 1)) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
else primes(n - 1); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
def is_collatz(n) = |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
if n == 1 then 1 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
else if n % 2 == 0 then is_collatz(n / 2) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
else is_collatz(3 * n + 1); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
def collatz_aux(n, i) = |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
if i > n then 0 |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
else if is_collatz(i) == 1 then (write i; collatz_aux(n, i + 1)) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
else collatz_aux(n, i + 1); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
def collatz(n) = collatz_aux(n, 1); |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
656 | 62 |
def facT(n, acc) = |
63 |
if n == 0 then acc else facT(n - 1, n * acc); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
//zero(3) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
//suc(8) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
//pred(7) |
656 | 69 |
//write(add(3, 4)) |
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
//mult(4,5) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
//pow(2, 3) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
//fib(20) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
//(write(fact(5)) ; fact(6)) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
//(write(1) ; 2) |
656 | 75 |
//write(ack(3, 12)) // for tail-rec test |
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
//stack_test(0) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
//(write (div(11, 3)); rem(11, 3)) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
//gcd(54, 24) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
//is_prime(2) |
695
484b74bc057e
deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents:
656
diff
changeset
|
80 |
primes(1000) |
484b74bc057e
deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents:
656
diff
changeset
|
81 |
//primes(1000000) |
484b74bc057e
deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents:
656
diff
changeset
|
82 |
//collatz(4000) |
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
//collatz(5000) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
//facT(6, 1) |
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |