diff --git a/alan/programs/knapsack.alan b/alan/programs/knapsack.alan index 8e0e89d..5700558 100644 --- a/alan/programs/knapsack.alan +++ b/alan/programs/knapsack.alan @@ -1,33 +1,36 @@ -knapsack(n: int, w_max: int, w: reference int [], v: reference int []) : int - dp : int[10000]; - i : int; - w_c : int; -{ - w_c = 0; - while (w_c<=w_max) { - dp[w_c] = 0; - w_c = w_c+1; - } - i = 0; - while (i0) { - if (w[i] <= w_c and v[i] + dp[w_c-w[i]] > dp[w_c]) dp[w_c] = v[i] + dp[w_c-w[i]]; - w_c = w_c - 1; +main () : proc + + knapsack(n: int, w_max: int, w: reference int [], v: reference int []) : int + dp : int[10000]; + i : int; + w_c : int; + { + w_c = 0; + while (w_c <= w_max) { + dp[w_c] = 0; + w_c = w_c+1; } - i = i+1; + i = 0; + while (i < n) { + w_c = w_max; + while (w_c > 0) { + if (w[i] <= w_c & v[i] + dp[w_c-w[i]] > dp[w_c]) + dp[w_c] = v[i] + dp[w_c-w[i]]; + w_c = w_c - 1; + } + i = i+1; + } + return dp[w_max]; } - return dp[w_max]; -} - -main () : proc w : int[10000]; v : int[10000]; n : int; w_max : int; res : int; -{ + i : int; + +{ -- main n = readInteger(); w_max = readInteger(); @@ -49,5 +52,5 @@ main () : proc writeInteger(res); writeChar('\n'); -} +} -- main diff --git a/alan/programs/mergesort.alan b/alan/programs/mergesort.alan index 4b3c126..c0e971b 100644 --- a/alan/programs/mergesort.alan +++ b/alan/programs/mergesort.alan @@ -1,3 +1,5 @@ +main () : proc + merge (x: reference int [], start: int, mid: int, end: int) : proc tmp: int[10000]; tmp_idx: int; @@ -10,7 +12,7 @@ merge (x: reference int [], start: int, mid: int, end: int) : proc l_itr = start; r_itr = mid; - while (l_itr < mid and r_itr < end) { + while (l_itr < mid & r_itr < end) { if (x[l_itr] < x[r_itr]) { tmp[tmp_idx] = x[l_itr]; l_itr = l_itr+1; @@ -33,24 +35,23 @@ merge (x: reference int [], start: int, mid: int, end: int) : proc x_itr = x_itr+1; tmp_idx2 = tmp_idx2+1; } +} - -mergesort (x: reference int [], int l, int r) : proc +mergeSort (x: reference int [], l: int, r: int) : proc { -- sort [l,r) segment if (l == r-1) return; - mergesort(x, l, (l+r)/2); - mergesort(x, (l+r)/2, r); + mergeSort(x, l, (l+r)/2); + mergeSort(x, (l+r)/2, r); -- merge (separate function to allocate local variables in them and reduce stack consumption) merge(x, l, (l+r)/2, r); } -main () : proc x : int [10000]; n : int; i : int; -{ +{ -- main n = readInteger(); if (n <= 0) return; diff --git a/alan/programs/mergesort_recursive.alan b/alan/programs/mergesort_recursive.alan index c440146..214ebd0 100644 --- a/alan/programs/mergesort_recursive.alan +++ b/alan/programs/mergesort_recursive.alan @@ -1,4 +1,4 @@ -merge (x: reference int [], start: int, mid: int, end int) : proc +merge (x: reference int [], start: int, mid: int, end: int) : proc -- through recursion create a "recursive array" in callstack (through the 'cur' variables). aux (x_itr: int, l: int, r: int) : proc -- presumably (given the scope rules) x, mid, end are visible inside aux. @@ -6,7 +6,7 @@ merge (x: reference int [], start: int, mid: int, end int) : proc { if (x_itr >= end) return; - if (l < mid and r < end) { + if (l < mid & r < end) { if (x[l] < x[r]) { cur = x[l]; l = l+1; @@ -23,14 +23,13 @@ merge (x: reference int [], start: int, mid: int, end int) : proc x[x_itr] = cur; } aux(start, start, mid); -- no need for curly brackets as it is just one statement - -mergesort (x: reference int [], int l, int r) : proc +mergeSort (x: reference int [], l: int, r: int) : proc { -- sort [l,r) segment if (l == r-1) return; - mergesort(x, l, (l+r)/2); - mergesort(x, (l+r)/2, r); + mergeSort(x, l, (l+r)/2); + mergeSort(x, (l+r)/2, r); -- merge (separate function to allocate local variables in them and reduce stack consumption) merge(x, l, (l+r)/2, r); diff --git a/alan/programs/powint.alan b/alan/programs/powint.alan index eb4b5d7..716739a 100644 --- a/alan/programs/powint.alan +++ b/alan/programs/powint.alan @@ -1,21 +1,22 @@ -powint (base : int, expon : int, mod : int) : int - res: int -{ - base = base % mod; - res = 1; - while (expon > 0) { - if (expon % 2 == 1) res = (res*base) % mod; - base = (base*base) % mod; - expon = expon / 2; +main () : proc + + powint (base : int, expon : int, mod : int) : int + res: int; + { + base = base % mod; + res = 1; + while (expon > 0) { + if (expon % 2 == 1) res = (res*base) % mod; + base = (base*base) % mod; + expon = expon / 2; + } + return res; } - return res; -} -main () : proc - b: int - e: int - m: int - res: int + b: int; + e: int; + m: int; + res: int; { b = readInteger(); e = readInteger();