(* -------------------------------------------------------------------- * * * * At one point in their history, the Egyptians did not have a general * * way of writing fractions as m/n. Instead, they wrote such fractions * * as the sums of k unit fractions 1/m, for different values of m. * * * * 3 1 1 * * They would write - as - + --, which we can abbreviate as [2, 10], * * 5 2 10 * * * * 12 1 1 1 1 * * and -- could be written as - + - + -- + ---, or [2, 3, 12, 156]. * * 13 2 3 12 156 * * * * This program prints the first k-1 denominators and returns the last * * denominator, which the executable prints. * * * * https://www.cs.uni.edu/~wallingf/teaching/cs4550/sessions/10.html * * #a-klein-program-to-write * * -------------------------------------------------------------------- *) function main(m: integer, n: integer): integer if (m = 1) then n else if MOD(n, m) = 0 then n / m else print_and_continue(m, n, n/m + 1) (* print the unit fraction that was found, *) (* then compute the fraction for m/n - 1/unit *) function print_and_continue(m: integer, n: integer, unit: integer): integer print(unit) main((unit * m) - n, n * unit) (* from the Klein library *) function MOD(m: integer, n: integer): integer m - m/n * n