Example programs
Short, complete programs. Each one is checked: we generate the code for every language, run it and compare the output.
Get all the examples
Clone the repository and open the folder in VS Code.
git clone https://github.com/scmc24/algoml-examples.gitBasics
average.aml
Average of three marks.Algorithm Averageversion '1.0'language Cvariable m1 : REALvariable m2 : REALvariable m3 : REALvariable mean : REALbeginread(m1)read(m2)read(m3)mean <- (m1 + m2 + m3) / 3.0write("Average: ", mean, "\n")endend algorithm
Algorithm Circleversion '1.0'language Cconstant PI : REAL = 3.14159variable radius : REALvariable area : REALbeginradius <- 2.0area <- PI * radius * radiuswrite("Area: ", area, "\n")endend algorithm
Output
Area: 12.56636circle.aml on GitHubTry itSoonduration.aml
Split seconds into hours, minutes and seconds.Algorithm Durationversion '1.0'language Cvariable total : INTvariable h : INTvariable m : INTvariable s : INTbeginread(total)h <- total div 3600m <- total mod 3600 div 60s <- total mod 60write(total, " seconds = ", h, " h ", m, " min ", s, " s\n")endend algorithm
Algorithm Greetingversion '1.0'language Cvariable name : STRINGvariable age : INTbeginwrite("What is your name? ")read(name)write("How old are you? ")read(age)write("Hello ", name, ", next year you will be ", age + 1, ".\n")endend algorithm
Input
Ada 36Output What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml on GitHubTry itSoonAlgorithm Helloversion '1.0'language Cbeginwrite("Hello, world!\n")endend algorithm
Output
Hello, world!hello.aml on GitHubTry itSoonAlgorithm Minutesversion '1.0'language Cvariable total : INTvariable hours : INTvariable minutes : INTbeginread(total)hours <- total div 60minutes <- total mod 60write(total, " minutes = ", hours, " h ", minutes, " min\n")endend algorithm
swap.aml
Swap two variables with a third one.Algorithm Swapversion '1.0'language Cvariable a : INTvariable b : INTvariable tmp : INTbegina <- 4b <- 9write("Before: a = ", a, ", b = ", b, "\n")tmp <- aa <- bb <- tmpwrite("After: a = ", a, ", b = ", b, "\n")endend algorithm
Output
Before: a = 4, b = 9 After: a = 9, b = 4swap.aml on GitHubTry itSoontemperature.aml
Convert Celsius to Fahrenheit.Algorithm Temperatureversion '1.0'language Cvariable celsius : REALvariable fahrenheit : REALbeginread(celsius)fahrenheit <- celsius * 9.0 / 5.0 + 32.0write(celsius, " C = ", fahrenheit, " F\n")endend algorithm
Conditions
calculator.aml
Four operations chosen from a menu.Algorithm Calculatorversion '1.0'language Cvariable a : INTvariable b : INTvariable choice : INTbeginread(a)read(b)read(choice)switch (choice) :case 1 :write(a, " + ", b, " = ", a + b, "\n")case 2 :write(a, " - ", b, " = ", a - b, "\n")case 3 :write(a, " x ", b, " = ", a * b, "\n")case 4 :if b = 0 thenwrite("Division by zero\n")elsewrite(a, " / ", b, " = ", a / b, "\n")end ifdefault :write("Unknown operation\n")end switchendend algorithm
even_or_odd.aml
Is a number even or odd?Algorithm EvenOrOddversion '1.0'language Cvariable n : INTbeginread(n)if n mod 2 = 0 thenwrite(n, " is even\n")elsewrite(n, " is odd\n")end ifendend algorithm
Algorithm Gradeversion '1.0'language Cvariable mark : INTbeginread(mark)if mark >= 16 thenwrite("Very good\n")elseif mark >= 12 thenwrite("Good\n")elseif mark >= 10 thenwrite("Pass\n")elsewrite("Fail\n")end ifendend algorithm
leap_year.aml
Is a year a leap year?Algorithm LeapYearversion '1.0'language Cvariable year : INTbeginread(year)if (year mod 4 = 0 AND year mod 100 <> 0) OR year mod 400 = 0 thenwrite(year, " is a leap year\n")elsewrite(year, " is not a leap year\n")end ifendend algorithm
Algorithm MaxOfThreeversion '1.0'language Cvariable a : INTvariable b : INTvariable c : INTvariable max : INTbeginread(a)read(b)read(c)max <- aif b > max thenmax <- bend ifif c > max thenmax <- cend ifwrite("The largest is ", max, "\n")endend algorithm
Algorithm Operatorsversion '1.0'language Cvariable a : INTvariable b : INTvariable even : BOOLEANbegina <- 17b <- 5write(a div b, "\n")write(a mod b, "\n")even <- a mod 2 = 0if NOT even AND a > b thenwrite("a is odd and greater than b\n")end ifa +<- 3write(a, "\n")endend algorithm
Output
3 2 a is odd and greater than b 20operators.aml on GitHubTry itSoonsign.aml
Positive, negative or zero.Algorithm Signversion '1.0'language Cvariable x : INTbeginread(x)if x > 0 thenwrite("positive\n")elseif x < 0 thenwrite("negative\n")elsewrite("zero\n")end ifendend algorithm
triangle.aml
Type of a triangle from its three sides.Algorithm Triangleversion '1.0'language Cvariable a : INTvariable b : INTvariable c : INTbeginread(a)read(b)read(c)if a + b <= c OR a + c <= b OR b + c <= a thenwrite("Not a triangle\n")elseif a = b AND b = c thenwrite("Equilateral\n")elseif a = b OR b = c OR a = c thenwrite("Isosceles\n")elsewrite("Scalene\n")end ifendend algorithm
Algorithm Weekdayversion '1.0'language Cvariable day : INTbeginread(day)switch (day) :case 6 :write("Saturday: weekend\n")case 7 :write("Sunday: weekend\n")default :write("Weekday\n")end switchendend algorithm
Loops
collatz.aml
Steps of the Collatz sequence down to 1.Algorithm Collatzversion '1.0'language Cvariable n : INTvariable steps : INTbeginread(n)steps <- 0while n <> 1 doif n mod 2 = 0 thenn <- n div 2elsen <- 3 * n + 1end ifsteps <- steps + 1end whilewrite("Reached 1 in ", steps, " steps\n")endend algorithm
countdown.aml
Count down with while.Algorithm Countdownversion '1.0'language Cvariable n : INTbeginn <- 5while n > 0 dowrite(n, "\n")n <- n - 1end whilewrite("Go!\n")endend algorithm
Output
5 4 3 2 1 Go!countdown.aml on GitHubTry itSoondigit_sum.aml
Sum of the digits of a number.Algorithm DigitSumversion '1.0'language Cvariable n : INTvariable total : INTbeginread(n)total <- 0while n > 0 dototal <- total + n mod 10n <- n div 10end whilewrite("Sum of the digits: ", total, "\n")endend algorithm
Algorithm EvenNumbersversion '1.0'language Cvariable i : INTbeginfor i in 1 to 10 dowrite(2 * i, "\n")end forendend algorithm
Output
2 4 6 8 10 12 14 16 18 20even_numbers.aml on GitHubTry itSoonAlgorithm Loopsversion '1.0'language Cvariable i : INTvariable n : INTbeginn <- 3for i in 1 to n dowrite(i, "\n")end forfor i in 1 to 9 step 2 dowrite(i, "\n")end forendend algorithm
Output
1 2 3 1 3 5 7 9loops.aml on GitHubTry itSoonpassword.aml
Ask for a password until it is right.Algorithm Passwordversion '1.0'language Cvariable word : STRINGvariable tries : INTbegintries <- 0repeatwrite("Password: ")read(word)tries <- tries + 1until word = "algoml"write("Welcome! ", tries, " tries\n")endend algorithm
Input
hello AlgoML algomlOutput Password: Password: Password: Welcome! 3 triespassword.aml on GitHubTry itSoonAlgorithm Positiveversion '1.0'language Cvariable n : INTbeginrepeatwrite("Enter a positive number: ")read(n)until n > 0write("Thank you, you entered ", n, "\n")endend algorithm
Input
-4 0 9Output Enter a positive number: Enter a positive number: Enter a positive number: Thank you, you entered 9positive.aml on GitHubTry itSoonpower.aml
Power of a number with a loop.Algorithm Powerversion '1.0'language Cvariable base : INTvariable exponent : INTvariable result : INTvariable i : INTbeginread(base)read(exponent)result <- 1for i in 1 to exponent doresult <- result * baseend forwrite(base, "^", exponent, " = ", result, "\n")endend algorithm
reverse_number.aml
Write a number backwards.Algorithm ReverseNumberversion '1.0'language Cvariable n : INTvariable reversed : INTbeginread(n)reversed <- 0while n > 0 doreversed <- reversed * 10 + n mod 10n <- n div 10end whilewrite(reversed, "\n")endend algorithm
stars.aml
A triangle of stars with nested loops.Algorithm Starsversion '1.0'language Cvariable row : INTvariable col : INTbeginfor row in 1 to 5 dofor col in 1 to row dowrite("*")end forwrite("\n")end forendend algorithm
Output
* ** *** **** *****stars.aml on GitHubTry itSoonsum_to_n.aml
Sum of the numbers from 1 to n.Algorithm SumToNversion '1.0'language Cvariable n : INTvariable i : INTvariable total : INTbeginread(n)total <- 0for i in 1 to n dototal <- total + iend forwrite("1 + 2 + ... + ", n, " = ", total, "\n")endend algorithm
Functions and procedures
absolute.aml
Absolute value of a REAL.Algorithm Absoluteversion '1.0'language Cfunction absolute(x : REAL) : REALbeginif x < 0.0 thenreturn -xend ifreturn xend functionbeginwrite(absolute(-3.5), "\n")write(absolute(2.25), "\n")endend algorithm
Output
3.5 2.25absolute.aml on GitHubTry itSoonAlgorithm Factorialversion '1.0'language Cfunction factorial(n : INT) : INTbeginif n <= 1 thenreturn 1end ifreturn n * factorial(n - 1)end functionvariable n : INTvariable result : INTbeginread(n)result <- factorial(n)write(n, "! = ", result, "\n")endend algorithm
fibonacci.aml
The first ten Fibonacci numbers.Algorithm Fibonacciversion '1.0'language Cfunction fib(n : INT) : INTvariable a : INTvariable b : INTvariable tmp : INTvariable i : INTbegina <- 0b <- 1for i in 1 to n dotmp <- a + ba <- bb <- tmpend forreturn aend functionvariable i : INTbeginfor i in 1 to 10 dowrite(fib(i), "\n")end forendend algorithm
Output
1 1 2 3 5 8 13 21 34 55fibonacci.aml on GitHubTry itSoonAlgorithm Gcdversion '1.0'language Cfunction gcd(a : INT, b : INT) : INTvariable r : INTbeginwhile b <> 0 dor <- a mod ba <- bb <- rend whilereturn aend functionvariable x : INTvariable y : INTbeginread(x)read(y)write("gcd = ", gcd(x, y), "\n")endend algorithm
hanoi.aml
Towers of Hanoi with a recursive procedure.Algorithm Hanoiversion '1.0'language Cprocedure move(n : INT, source : STRING, target : STRING, spare : STRING)beginif n > 0 thenmove(n - 1, source, spare, target)write("Move disk ", n, " from ", source, " to ", target, "\n")move(n - 1, spare, target, source)end ifend procedurebeginmove(3, "A", "C", "B")endend algorithm
Output
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to Chanoi.aml on GitHubTry itSoonAlgorithm IsPrimeversion '1.0'language Cfunction isPrime(n : INT) : BOOLEANvariable d : INTbeginif n < 2 thenreturn Falseend ifd <- 2while d * d <= n doif n mod d = 0 thenreturn Falseend ifd <- d + 1end whilereturn Trueend functionvariable n : INTbeginfor n in 1 to 20 doif isPrime(n) thenwrite(n, "\n")end ifend forendend algorithm
Output
2 3 5 7 11 13 17 19is_prime.aml on GitHubTry itSoonlcm.aml
Least common multiple, using gcd.Algorithm Lcmversion '1.0'language Cfunction gcd(a : INT, b : INT) : INTvariable r : INTbeginwhile b <> 0 dor <- a mod ba <- bb <- rend whilereturn aend functionfunction lcm(a : INT, b : INT) : INTbeginreturn a div gcd(a, b) * bend functionbeginwrite("lcm(12, 18) = ", lcm(12, 18), "\n")endend algorithm
Output
lcm(12, 18) = 36lcm.aml on GitHubTry itSoonmin_max.aml
Two small functions: minimum and maximum.Algorithm MinMaxversion '1.0'language Cfunction minimum(a : INT, b : INT) : INTbeginif a < b thenreturn aend ifreturn bend functionfunction maximum(a : INT, b : INT) : INTbeginif a > b thenreturn aend ifreturn bend functionbeginwrite("min = ", minimum(14, 9), ", max = ", maximum(14, 9), "\n")endend algorithm
Output
min = 9, max = 14min_max.aml on GitHubTry itSoonpower_recursive.aml
Power of a number, recursive version.Algorithm PowerRecursiveversion '1.0'language Cfunction power(x : INT, n : INT) : INTbeginif n = 0 thenreturn 1end ifreturn x * power(x, n - 1)end functionbeginwrite("2^10 = ", power(2, 10), "\n")endend algorithm
Output
2^10 = 1024power_recursive.aml on GitHubTry itSoonAlgorithm Tableversion '1.0'language Cprocedure printTable(n : INT)variable i : INTbeginfor i in 1 to 5 dowrite(n, " x ", i, " = ", n * i, "\n")end forend procedurebeginprintTable(7)endend algorithm
Output
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35table.aml on GitHubTry itSoonArrays
count_occurrences.aml
How many times a value appears.Algorithm CountOccurrencesversion '1.0'language Cvariable t : ARRAY[10] of INTvariable i : INTvariable target : INTvariable count : INTbegint <- [3, 7, 3, 1, 9, 3, 7, 2, 3, 8]read(target)count <- 0for i in 1 to 10 doif t[i] = target thencount <- count + 1end ifend forwrite(target, " appears ", count, " times\n")endend algorithm
Algorithm MaxInArrayversion '1.0'language Cvariable values : ARRAY[6] of INTvariable i : INTvariable max : INTbeginvalues <- [14, 3, 27, 8, 19, 5]max <- values[1]for i in 1 to 6 doif values[i] > max thenmax <- values[i]end ifend forwrite("Max: ", max, "\n")endend algorithm
Output
Max: 27max_in_array.aml on GitHubTry itSoonread_array.aml
Fill an array from the keyboard.Algorithm ReadArrayversion '1.0'language Cvariable values : ARRAY[5] of INTvariable x : INTvariable i : INTvariable total : INTbeginfor i in 1 to 5 doread(x)values[i] <- xend fortotal <- 0for i in 1 to 5 dototal <- total + values[i]end forwrite("Total: ", total, "\n")endend algorithm
reverse_array.aml
Reverse an array in place.Algorithm ReverseArrayversion '1.0'language Cvariable t : ARRAY[6] of INTvariable i : INTvariable tmp : INTbegint <- [1, 2, 3, 4, 5, 6]for i in 1 to 3 dotmp <- t[i]t[i] <- t[7 - i]t[7 - i] <- tmpend forfor i in 1 to 6 dowrite(t[i], "\n")end forendend algorithm
Output
6 5 4 3 2 1reverse_array.aml on GitHubTry itSoonsecond_largest.aml
Largest and second largest value.Algorithm SecondLargestversion '1.0'language Cvariable t : ARRAY[7] of INTvariable i : INTvariable first : INTvariable second : INTbegint <- [12, 45, 7, 45, 30, 18, 3]first <- t[1]second <- 0for i in 2 to 7 doif t[i] > first thensecond <- firstfirst <- t[i]elseif t[i] < first AND t[i] > second thensecond <- t[i]end ifend forwrite("Largest: ", first, ", second: ", second, "\n")endend algorithm
Output
Largest: 45, second: 30second_largest.aml on GitHubTry itSoonshift_left.aml
Rotate an array one cell to the left.Algorithm ShiftLeftversion '1.0'language Cvariable t : ARRAY[5] of INTvariable i : INTvariable first : INTbegint <- [10, 20, 30, 40, 50]first <- t[1]for i in 1 to 4 dot[i] <- t[i + 1]end fort[5] <- firstfor i in 1 to 5 dowrite(t[i], "\n")end forendend algorithm
Output
20 30 40 50 10shift_left.aml on GitHubTry itSoonAlgorithm SumAndMeanversion '1.0'language Cvariable marks : ARRAY[5] of INTvariable i : INTvariable total : INTbeginmarks <- [12, 15, 9, 18, 11]total <- 0for i in 1 to 5 dototal <- total + marks[i]end forwrite("Sum: ", total, "\n")write("Mean: ", total div 5, "\n")endend algorithm
Output
Sum: 65 Mean: 13sum_and_mean.aml on GitHubTry itSoonStructures
bank_account.aml
A bank account with a REAL balance.Algorithm BankAccountversion '1.0'language Cstructure Accountowner : STRINGbalance : REALend structurevariable acc : structure Accountbeginacc.owner <- "Ada"acc.balance <- 100.0acc.balance <- acc.balance + 50.5acc.balance <- acc.balance - 20.0write(acc.owner, " has ", acc.balance, " on her account\n")endend algorithm
Output
Ada has 130.5 on her accountbank_account.aml on GitHubTry itSoondate.aml
Which of two dates comes first.Algorithm DateCheckversion '1.0'language Cstructure Dateday : INTmonth : INTyear : INTend structurefunction before(d1 : structure Date, d2 : structure Date) : INTbeginif d1.year <> d2.year thenif d1.year < d2.year thenreturn 1end ifreturn 0end ifif d1.month <> d2.month thenif d1.month < d2.month thenreturn 1end ifreturn 0end ifif d1.day < d2.day thenreturn 1end ifreturn 0end functionvariable a : structure Datevariable b : structure Datebegina.day <- 14a.month <- 7a.year <- 2025b.day <- 2b.month <- 3b.year <- 2026if before(a, b) = 1 thenwrite("The first date comes first\n")elsewrite("The second date comes first\n")end ifendend algorithm
Output
The first date comes firstdate.aml on GitHubTry itSoonpoint.aml
Squared distance between two points.Algorithm PointDistanceversion '1.0'language Cstructure Pointx : INTy : INTend structurefunction distanceSquared(p : structure Point, q : structure Point) : INTbeginreturn (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y)end functionvariable a : structure Pointvariable b : structure Pointbegina.x <- 1a.y <- 2b.x <- 4b.y <- 6write("Distance squared: ", distanceSquared(a, b), "\n")endend algorithm
Output
Distance squared: 25point.aml on GitHubTry itSoonAlgorithm RectangleAreaversion '1.0'language Cstructure Rectanglewidth : INTheight : INTend structurefunction area(r : structure Rectangle) : INTbeginreturn r.width * r.heightend functionvariable r : structure Rectanglebeginr.width <- 8r.height <- 5write("Area: ", area(r), "\n")endend algorithm
Output
Area: 40rectangle.aml on GitHubTry itSoonAlgorithm StudentCardversion '1.0'language Cstructure Studentname : STRINGmark : INTend structurevariable s : structure Studentbegins.name <- "Ada"s.mark <- 15write(s.name, " got ", s.mark, "/20\n")endend algorithm
Output
Ada got 15/20student.aml on GitHubTry itSoonSorting and searching
binary_search.aml
Binary search in a sorted array.Algorithm BinarySearchversion '1.0'language Cvariable t : ARRAY[10] of INTvariable low : INTvariable high : INTvariable middle : INTvariable target : INTvariable position : INTbegint <- [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]read(target)low <- 1high <- 10position <- 0while low <= high AND position = 0 domiddle <- (low + high) div 2if t[middle] = target thenposition <- middleelseif t[middle] < target thenlow <- middle + 1elsehigh <- middle - 1end ifend whileif position = 0 thenwrite(target, " not found\n")elsewrite(target, " found at position ", position, "\n")end ifendend algorithm
Algorithm BubbleSortversion '1.0'language Cvariable tab : ARRAY[6] of INTvariable i : INTvariable j : INTvariable tmp : INTbegintab <- [5, 2, 9, 1, 7, 3]for i in 1 to 5 dofor j in 1 to 6 - i doif tab[j] > tab[j + 1] thentmp <- tab[j]tab[j] <- tab[j + 1]tab[j + 1] <- tmpend ifend forend forfor i in 1 to 6 dowrite(tab[i], "\n")end forendend algorithm
Output
1 2 3 5 7 9bubble_sort.aml on GitHubTry itSooninsertion_sort.aml
Insertion sort.Algorithm InsertionSortversion '1.0'language Cvariable t : ARRAY[6] of INTvariable i : INTvariable j : INTvariable key : INTbegint <- [31, 41, 59, 26, 41, 58]for i in 2 to 6 dokey <- t[i]j <- i - 1while j >= 1 AND t[j] > key dot[j + 1] <- t[j]j <- j - 1end whilet[j + 1] <- keyend forfor i in 1 to 6 dowrite(t[i], "\n")end forendend algorithm
Output
26 31 41 41 58 59insertion_sort.aml on GitHubTry itSoonlinear_search.aml
Find a value by going through the array.Algorithm LinearSearchversion '1.0'language Cvariable t : ARRAY[8] of INTvariable i : INTvariable target : INTvariable position : INTbegint <- [14, 3, 27, 8, 19, 5, 42, 11]read(target)position <- 0i <- 1while i <= 8 AND position = 0 doif t[i] = target thenposition <- iend ifi <- i + 1end whileif position = 0 thenwrite(target, " not found\n")elsewrite(target, " found at position ", position, "\n")end ifendend algorithm
selection_sort.aml
Selection sort.Algorithm SelectionSortversion '1.0'language Cvariable t : ARRAY[7] of INTvariable i : INTvariable j : INTvariable m : INTvariable tmp : INTbegint <- [29, 10, 14, 37, 13, 5, 21]for i in 1 to 6 dom <- ifor j in i + 1 to 7 doif t[j] < t[m] thenm <- jend ifend fortmp <- t[i]t[i] <- t[m]t[m] <- tmpend forfor i in 1 to 7 dowrite(t[i], "\n")end forendend algorithm
Output
5 10 13 14 21 29 37selection_sort.aml on GitHubTry itSoonNumber problems
armstrong.aml
Three-digit Armstrong numbers.Algorithm Armstrongversion '1.0'language Cvariable n : INTvariable x : INTvariable digit : INTvariable total : INTbeginfor n in 100 to 999 dox <- ntotal <- 0while x > 0 dodigit <- x mod 10total <- total + digit * digit * digitx <- x div 10end whileif total = n thenwrite(n, "\n")end ifend forendend algorithm
Output
153 370 371 407armstrong.aml on GitHubTry itSoondivisors.aml
All the divisors of a number.Algorithm Divisorsversion '1.0'language Cvariable n : INTvariable d : INTbeginread(n)for d in 1 to n doif n mod d = 0 thenwrite(d, "\n")end ifend forendend algorithm
pascal.aml
Pascal's triangle, one array updated row by row.Algorithm Pascalversion '1.0'language Cvariable row : ARRAY[7] of INTvariable n : INTvariable k : INTbeginfor n in 1 to 7 dorow[n] <- 1k <- n - 1while k >= 2 dorow[k] <- row[k] + row[k - 1]k <- k - 1end whilefor k in 1 to n dowrite(row[k], " ")end forwrite("\n")end forendend algorithm
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1pascal.aml on GitHubTry itSoonperfect_numbers.aml
Perfect numbers below 1000.Algorithm PerfectNumbersversion '1.0'language Cfunction divisorSum(n : INT) : INTvariable d : INTvariable total : INTbegintotal <- 0for d in 1 to n - 1 doif n mod d = 0 thentotal <- total + dend ifend forreturn totalend functionvariable n : INTbeginfor n in 2 to 1000 doif divisorSum(n) = n thenwrite(n, "\n")end ifend forendend algorithm
Output
6 28 496perfect_numbers.aml on GitHubTry itSoonprime_factors.aml
Prime factorisation.Algorithm PrimeFactorsversion '1.0'language Cvariable n : INTvariable d : INTbeginread(n)d <- 2while n > 1 dowhile n mod d = 0 dowrite(d, "\n")n <- n div dend whiled <- d + 1end whileendend algorithm
sieve.aml
Sieve of Eratosthenes up to 50.Algorithm Sieveversion '1.0'language Cvariable crossed : ARRAY[50] of INTvariable i : INTvariable j : INTbeginfor i in 1 to 50 docrossed[i] <- 0end forfor i in 2 to 50 doif crossed[i] = 0 thenwrite(i, "\n")j <- i * iwhile j <= 50 docrossed[j] <- 1j <- j + iend whileend ifend forendend algorithm
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47sieve.aml on GitHubTry itSoon