From 6c7b7fe84b97b27a1ab2f15f46744ac3dd32958f Mon Sep 17 00:00:00 2001 From: Stavros Avramidis Date: Thu, 29 Aug 2024 21:37:02 +0300 Subject: [PATCH] [alan] Adds sieve and fizzbuzz programs and 42 Signed-off-by: Stavros Avramidis --- alan/programs/answer.alan | 9 ++++++ alan/programs/answer.input | 1 + alan/programs/answer.result | 1 + alan/programs/fizzbuzz.alan | 24 ++++++++++++++++ alan/programs/fizzbuzz.result | 1 + alan/programs/sieve.alan | 53 +++++++++++++++++++++++++++++++++++ alan/programs/sieve.result | 1 + 7 files changed, 90 insertions(+) create mode 100644 alan/programs/answer.alan create mode 100644 alan/programs/answer.input create mode 100644 alan/programs/answer.result create mode 100644 alan/programs/fizzbuzz.alan create mode 100644 alan/programs/fizzbuzz.result create mode 100644 alan/programs/sieve.alan create mode 100644 alan/programs/sieve.result diff --git a/alan/programs/answer.alan b/alan/programs/answer.alan new file mode 100644 index 0000000..ff764d3 --- /dev/null +++ b/alan/programs/answer.alan @@ -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"); + } +} diff --git a/alan/programs/answer.input b/alan/programs/answer.input new file mode 100644 index 0000000..d81cc07 --- /dev/null +++ b/alan/programs/answer.input @@ -0,0 +1 @@ +42 diff --git a/alan/programs/answer.result b/alan/programs/answer.result new file mode 100644 index 0000000..2993c11 --- /dev/null +++ b/alan/programs/answer.result @@ -0,0 +1 @@ +The answer to the ultimate question of Life, the Universe, and Everything is 42. diff --git a/alan/programs/fizzbuzz.alan b/alan/programs/fizzbuzz.alan new file mode 100644 index 0000000..291f77b --- /dev/null +++ b/alan/programs/fizzbuzz.alan @@ -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'); +} diff --git a/alan/programs/fizzbuzz.result b/alan/programs/fizzbuzz.result new file mode 100644 index 0000000..6a85cba --- /dev/null +++ b/alan/programs/fizzbuzz.result @@ -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 diff --git a/alan/programs/sieve.alan b/alan/programs/sieve.alan new file mode 100644 index 0000000..0ec30ef --- /dev/null +++ b/alan/programs/sieve.alan @@ -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'); +} diff --git a/alan/programs/sieve.result b/alan/programs/sieve.result new file mode 100644 index 0000000..345a61f --- /dev/null +++ b/alan/programs/sieve.result @@ -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