Programmes d'exemple
Des programmes courts et complets. Chacun est vérifié : on génère le code dans chaque langage, on l'exécute et on compare la sortie.
Récupérer tous les exemples
Clonez le dépôt et ouvrez le dossier dans VS Code.
git clone https://github.com/scmc24/algoml-examples.gitBases
average.aml
Moyenne de trois notes.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
Sortie
Area: 12.56636circle.aml sur GitHubEssayerBientôtduration.aml
Découper des secondes en heures, minutes et secondes.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
Entrée
Ada 36Sortie What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml sur GitHubEssayerBientôtAlgorithm Helloversion '1.0'language Cbeginwrite("Hello, world!\n")endend algorithm
Sortie
Hello, world!hello.aml sur GitHubEssayerBientôtAlgorithm 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
Échanger deux variables avec une troisième.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
Sortie
Before: a = 4, b = 9 After: a = 9, b = 4swap.aml sur GitHubEssayerBientôttemperature.aml
Convertir des degrés Celsius en 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
Les quatre opérations choisies dans un 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
Un nombre est-il pair ou impair ?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
Une année est-elle bissextile ?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
Sortie
3 2 a is odd and greater than b 20operators.aml sur GitHubEssayerBientôtsign.aml
Positif, négatif ou nul.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 d'un triangle à partir de ses trois côtés.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
Boucles
collatz.aml
Étapes de la suite de Syracuse jusqu'à 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
Compte à rebours avec while.Algorithm Countdownversion '1.0'language Cvariable n : INTbeginn <- 5while n > 0 dowrite(n, "\n")n <- n - 1end whilewrite("Go!\n")endend algorithm
Sortie
5 4 3 2 1 Go!countdown.aml sur GitHubEssayerBientôtdigit_sum.aml
Somme des chiffres d'un nombre.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
Sortie
2 4 6 8 10 12 14 16 18 20even_numbers.aml sur GitHubEssayerBientôtAlgorithm 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
Sortie
1 2 3 1 3 5 7 9loops.aml sur GitHubEssayerBientôtpassword.aml
Demander un mot de passe jusqu'au bon.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
Entrée
hello AlgoML algomlSortie Password: Password: Password: Welcome! 3 triespassword.aml sur GitHubEssayerBientôtAlgorithm Positiveversion '1.0'language Cvariable n : INTbeginrepeatwrite("Enter a positive number: ")read(n)until n > 0write("Thank you, you entered ", n, "\n")endend algorithm
Entrée
-4 0 9Sortie Enter a positive number: Enter a positive number: Enter a positive number: Thank you, you entered 9positive.aml sur GitHubEssayerBientôtpower.aml
Puissance d'un nombre avec une boucle.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
Écrire un nombre à l'envers.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
Un triangle d'étoiles avec des boucles imbriquées.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
Sortie
* ** *** **** *****stars.aml sur GitHubEssayerBientôtsum_to_n.aml
Somme des nombres de 1 à 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
Fonctions et procédures
absolute.aml
Valeur absolue d'un 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
Sortie
3.5 2.25absolute.aml sur GitHubEssayerBientôtAlgorithm 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
Les dix premiers nombres de Fibonacci.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
Sortie
1 1 2 3 5 8 13 21 34 55fibonacci.aml sur GitHubEssayerBientôtAlgorithm 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
Tours de Hanoï avec une procédure récursive.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
Sortie
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 sur GitHubEssayerBientôtis_prime.aml
Nombres premiers jusqu'à 20 avec une fonction BOOLEAN.Expliqué dans Fonctions et procéduresAlgorithm 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
Sortie
2 3 5 7 11 13 17 19is_prime.aml sur GitHubEssayerBientôtlcm.aml
Plus petit commun multiple, à partir du pgcd.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
Sortie
lcm(12, 18) = 36lcm.aml sur GitHubEssayerBientôtmin_max.aml
Deux petites fonctions : minimum et 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
Sortie
min = 9, max = 14min_max.aml sur GitHubEssayerBientôtpower_recursive.aml
Puissance d'un nombre, version récursive.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
Sortie
2^10 = 1024power_recursive.aml sur GitHubEssayerBientôtAlgorithm 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
Sortie
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35table.aml sur GitHubEssayerBientôtTableaux
count_occurrences.aml
Nombre d'apparitions d'une valeur.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
Sortie
Max: 27max_in_array.aml sur GitHubEssayerBientôtread_array.aml
Remplir un tableau au clavier.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
Inverser un tableau sur 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
Sortie
6 5 4 3 2 1reverse_array.aml sur GitHubEssayerBientôtsecond_largest.aml
Plus grande et deuxième plus grande valeur.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
Sortie
Largest: 45, second: 30second_largest.aml sur GitHubEssayerBientôtshift_left.aml
Décaler un tableau d'une case vers la gauche.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
Sortie
20 30 40 50 10shift_left.aml sur GitHubEssayerBientôtAlgorithm 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
Sortie
Sum: 65 Mean: 13sum_and_mean.aml sur GitHubEssayerBientôtStructures
bank_account.aml
Un compte bancaire avec un solde REAL.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
Sortie
Ada has 130.5 on her accountbank_account.aml sur GitHubEssayerBientôtdate.aml
Laquelle de deux dates vient en premier.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
Sortie
The first date comes firstdate.aml sur GitHubEssayerBientôtpoint.aml
Distance au carré entre deux 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
Sortie
Distance squared: 25point.aml sur GitHubEssayerBientôtAlgorithm 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
Sortie
Area: 40rectangle.aml sur GitHubEssayerBientôtAlgorithm 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
Sortie
Ada got 15/20student.aml sur GitHubEssayerBientôtTris et recherches
binary_search.aml
Recherche dichotomique dans un tableau trié.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
Sortie
1 2 3 5 7 9bubble_sort.aml sur GitHubEssayerBientôtinsertion_sort.aml
Tri par insertion.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
Sortie
26 31 41 41 58 59insertion_sort.aml sur GitHubEssayerBientôtlinear_search.aml
Chercher une valeur en parcourant le tableau.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
Tri par sélection.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
Sortie
5 10 13 14 21 29 37selection_sort.aml sur GitHubEssayerBientôtProblèmes sur les nombres
armstrong.aml
Nombres d'Armstrong à trois chiffres.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
Sortie
153 370 371 407armstrong.aml sur GitHubEssayerBientôtdivisors.aml
Tous les diviseurs d'un nombre.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
Triangle de Pascal, un tableau mis à jour ligne par ligne.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
Sortie
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 sur GitHubEssayerBientôtperfect_numbers.aml
Nombres parfaits inférieurs à 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
Sortie
6 28 496perfect_numbers.aml sur GitHubEssayerBientôtprime_factors.aml
Décomposition en facteurs premiers.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
Crible d'Ératosthène jusqu'à 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
Sortie
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47sieve.aml sur GitHubEssayerBientôt