Skip to content

Commit

Permalink
Some fixes to Alan programs (probably more are needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
kostis committed May 19, 2024
1 parent 217d986 commit 9de4527
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
49 changes: 26 additions & 23 deletions alan/programs/knapsack.alan
Original file line number Diff line number Diff line change
@@ -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 (i<n) {
w_c = w_max;
while (w_c>0) {
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();

Expand All @@ -49,5 +52,5 @@ main () : proc

writeInteger(res);
writeChar('\n');
}
} -- main

15 changes: 8 additions & 7 deletions alan/programs/mergesort.alan
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
main () : proc

merge (x: reference int [], start: int, mid: int, end: int) : proc
tmp: int[10000];
tmp_idx: int;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions alan/programs/mergesort_recursive.alan
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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.
cur : int;
{
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;
Expand All @@ -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);
Expand Down
33 changes: 17 additions & 16 deletions alan/programs/powint.alan
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit 9de4527

Please sign in to comment.