Skip to content

Commit

Permalink
Add some Alan programs
Browse files Browse the repository at this point in the history
  • Loading branch information
kostis committed Feb 23, 2024
1 parent 9a71819 commit 83a13ae
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 0 deletions.
59 changes: 59 additions & 0 deletions alan/programs/bsort.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
main () : proc

bsort (n : int, x : reference int []) : proc

swap (x : reference int, y : reference int) : proc
t : int;
{
t = x;
x = y;
y = t;
}

changed : byte;
i : int;

{ -- bsort
changed = 'y';
while (changed == 'y') {
changed = 'n';
i = 0;
while (i < n-1) {
if (x[i] > x[i+1]) {
swap(x[i], x[i+1]);
changed = 'y';
}
i = i+1;
}
}
} -- bsort

writeArray (msg : reference byte [], n : int, x : reference int []) : proc
i : int;
{
writeString(msg);
i = 0;
while (i < n) {
if (i > 0) writeString(", ");
writeInteger(x[i]);
i = i+1;
}
writeString("\n");
}

seed : int;
x : int [16];
i : int;

{ -- main
seed = 65;
i = 0;
while (i < 16) {
seed = (seed * 137 + 220 + i) % 101;
x[i] = seed;
i = i+1;
}
writeArray("Initial array: ", 16, x);
bsort(16, x);
writeArray("Sorted array: ", 16, x);
} -- main
31 changes: 31 additions & 0 deletions alan/programs/cancer.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cancer(): proc

Check warning on line 1 in alan/programs/cancer.alan

View workflow job for this annotation

GitHub Actions / codespell

Cannot decode file using encoding "utf-8"
is_it (n:int, source:reference byte[]):int
i:int;

{
n=n-1;
i=0;
while ( i < (n/2 + 1) )
{
if(source[i]!=source[n-i])
return 1;
i=i+1;
}
return 0;
}

n:int;
source:byte[31];

{
writeString("Give a string with maximum length 30: ");
readString(30, source);
n=0;
while (source[n]!='\0')
n=n+1;
n=n-1;(*��������� �� n *)
if (is_it(n, source)==1) --�������� ���������� ���������
writeString("\nIs not cancern...");
else
writeString("\nIs cancern...");
}
20 changes: 20 additions & 0 deletions alan/programs/factors.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
main() : proc
n : int;
lcv : int;
flag : int;

{
writeString("Enter value of N > ");
n = readInteger();
lcv = 2; flag = 1;
while (lcv <= (n / 2))
{
if ((n % lcv) == 0) {
if (flag != 0) { writeString("The non-trivial factors of "); writeInteger(n); writeString(" are: \n"); }
flag = 0;
writeInteger(lcv); writeChar('\n');
}
lcv = lcv + 1;
}
if (flag != 0 ) { writeInteger(n); writeString(" is prime\n"); }
}
30 changes: 30 additions & 0 deletions alan/programs/gcd.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
gcd():proc
find_gcd(a:int, b:int):int
i:int;
{
if (a>b)
i=a;
else
i=b;

while (i>1)
{
if ((a%i==0) & (b%i==0))
return i;
i=i-1;
}
return 1;

}

a:int;
b:int;

{
writeString("Give the first integer: ");
a=readInteger();
writeString("Give the second integer: ");
b=readInteger();
writeString("\nThe gcd you are looking for is ");
writeInteger(find_gcd(a, b));
}
29 changes: 29 additions & 0 deletions alan/programs/hanoi.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
solve () : proc

hanoi (rings : int, source : reference byte [],
target : reference byte [], auxiliary : reference byte []) : proc

move (source : reference byte [], target : reference byte[]) : proc
{
writeString("Moving from ");
writeString(source);
writeString(" to ");
writeString(target);
writeString(".\n");
}

{ -- hanoi
if (rings >= 1) {
hanoi(rings-1, source, auxiliary, target);
move(source, target);
hanoi(rings-1, auxiliary, target, source);
}
} -- hanoi

NumberOfRings : int;

{ -- solve
writeString("Rings: ");
NumberOfRings = readInteger();
hanoi(NumberOfRings, "left", "right", "middle");
} -- solve
4 changes: 4 additions & 0 deletions alan/programs/hello.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hello () : proc
{
writeString("Hello world!\n");
}
58 changes: 58 additions & 0 deletions alan/programs/primes.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
main () : proc

prime (n : int) : int
i : int;
{
if (n < 0) return prime(-n);
else if (n < 2) return 0;
else if (n == 2) return 1;
else if (n % 2 == 0) return 0;
else {
i = 3;
while (i <= n / 2) {
if (n % i == 0)
return 0;
i = i + 2;
}
return 1;
}
}

limit : int;
number : int;
counter : int;

{ -- main
writeString("Limit: ");
limit = readInteger();
writeString("Primes:\n");
counter = 0;
if (limit >= 2) {
counter = counter + 1;
writeInteger(2);
writeString("\n");
}
if (limit >= 3) {
counter = counter + 1;
writeInteger(3);
writeString("\n");
}
number = 6;
while (number <= limit) {
if (prime(number - 1) == 1) {
counter = counter + 1;
writeInteger(number - 1);
writeString("\n");
}
if (number != limit & prime(number + 1) == 1) {
counter = counter + 1;
writeInteger(number + 1);
writeString("\n");
}
number = number + 6;
}

writeString("\nTotal: ");
writeInteger(counter);
writeString("\n");
} -- main
21 changes: 21 additions & 0 deletions alan/programs/strrev.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
main () : proc

r : byte [32];

reverse (s : reference byte []) : proc
i : int;
l : int;
{
l = strlen(s);
i = 0;
while (i < l) {
r[i] = s[l-i-1];
i = i+1;
}
r[i] = '\0';
}

{ -- main
reverse("\n!dlrow olleH");
writeString(r);
} -- main

0 comments on commit 83a13ae

Please sign in to comment.