Skip to content

Commit

Permalink
[alan] Adds sieve and fizzbuzz programs and 42
Browse files Browse the repository at this point in the history
Signed-off-by: Stavros Avramidis <[email protected]>
  • Loading branch information
purpl3F0x committed Aug 29, 2024
1 parent 6ab9e01 commit 6c7b7fe
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
9 changes: 9 additions & 0 deletions alan/programs/answer.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
main(): proc
{
if (readInteger() == 42) {
writeString("The answer to the ultimate question of Life, the Universe, and Everything is 42.\n");
}
else {
writeString("I'm sorry, I don't understand the question.\n");
}
}
1 change: 1 addition & 0 deletions alan/programs/answer.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
1 change: 1 addition & 0 deletions alan/programs/answer.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The answer to the ultimate question of Life, the Universe, and Everything is 42.
24 changes: 24 additions & 0 deletions alan/programs/fizzbuzz.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(*
Fizz buzz is a group word game for children to teach them about division.
Players take turns to count incrementally, replacing any number divisible
by three with the word "fizz", and any number divisible by five with the
word "buzz", and any number divisible by both three and five with the word
"fizzbuzz".
*)

main(): proc
i : int;
{
i = 0;
while (i < 100) {
i = i + 1;
if (i % 3 == 0 & i % 5 == 0) writeString("FizzBuzz");
else if (i % 3 == 0) writeString("Fizz");
else if (i % 5 == 0) writeString("Buzz");
else writeInteger(i);

if (i != 100)
writeChar(' ');
}
writeChar('\n');
}
1 change: 1 addition & 0 deletions alan/programs/fizzbuzz.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
53 changes: 53 additions & 0 deletions alan/programs/sieve.alan
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(*
The Sieve of Eratosthenes is an ancient and efficient algorithm used to find all
prime numbers up to a specified integer, n. The algorithm works by iteratively
marking the multiples of each prime number starting from 2, the smallest prime.
It begins by assuming that all numbers up to n are prime, then eliminates the
multiples of each prime by marking them as non-prime. The process continues for
each number up to the square root of n. After all multiples are marked, the
remaining unmarked numbers are prime. This method is particularly effective due
to its simplicity and ability to handle large ranges with minimal computational
resources.
*)

main(): proc
n : int;
i : int;
j : int;
primes : byte[100];
{
n = 100;

-- Initialize all entries in primes[] to true
i = 0;
while (i < n) {
primes[i] = shrink(1);
i = i + 1;
}

i = 2;
while (i * i <= n) {
if (primes[i] == shrink(1)) {
-- Mark all multiples of i as false (not prime)
j = i * i;
while (j <= n) {
primes[j] = shrink(0);
j = j + i;
}
}
i = i + 1;
}

-- Print all prime numbers
i = 2;
while (i <= n) {
if (primes[i] == shrink(1)){
writeInteger(i);
writeChar(' ');
}

i = i + 1;
}

writeChar('\n');
}
1 change: 1 addition & 0 deletions alan/programs/sieve.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

0 comments on commit 6c7b7fe

Please sign in to comment.