[Farey sequence for Rosetta Code website. EDSAC program, Initial Orders 2. Prints Farey sequences up to order 11 (or other limit determined by a simple edit).] [Modification of library subroutine P6. Prints number (absolute value <= 65535) passed in 0F, without leading spaces. 41 locations.] T 56 K GKA3FT35@SFG11@UFS40@E10@O40@T4FE35@O@T4F H38@VFT4DA13@TFH39@S16@T1FV4DU4DAFG36@TFTF O5FA4DF4FS4FL4FT4DA1FS13@G19@EFSFE30@J995FJFPD T 100 K G K [Maximum order to be printed. For convenience, entered as an address, not an integer, e.g. 'P 11 F' not 'P 5 D'.] [0] P 11 F [<--- edit here] [Other constants] [1] P D [1] [2] # F [figure shift] [3] X F [slash] [4] ! F [space] [5] @ F [carriage return] [6] & F [line feed] [7] K4096 F [teleprinter null] [Variables] [8] P F [n, order of current Farey sequence] [9] P F [maximum n + 1, as integer] [a/b and c/d are consecutive terms of the Farey sequence] [10] P F [a] [11] P F [b] [12] P F [c] [13] P F [d] [14] P F [t, temporary store] [Subroutine to print c/d] [15] A 3 F [plant link for return] T 26 @ A 12 @ [load c] T F [to 0F for printing] [19] A 19 @ [for subroutine return] G 56 F [print c] O 3 @ [print '/'] A 13 @ [load d] T F [to 0F for printing] [24] A 24 @ [for subroutine return] G 56 F [print d] [26] E F [return] [Main routine. Enter with accumulator = 0.] [27] O 2 @ [set teleprinter to figures] A @ [max order as address] R D [shift 1 right to make integer] A 1 @ [add 1] T 9 @ [save for comparison] A 1 @ [start with order 1] [Here with next order (n) in the accumulator] [33] S 9 @ [subtract (max order) + 1] E 84 @ [exit if over maximum] A 9 @ [restore after test] T 8 @ [store] [Prefix the Farey sequence with a formal term -1/0. The second term is calculated from this and the first term.] S 1 @ [acc := -1] T 10 @ [a := -1] T 11 @ [b := 0] T 12 @ [c := 0] A 1 @ [d := 1] T 13 @ A 43 @ [for subroutine return] G 15 @ [call subroutine to print c/d] [Calculate next term; basically same as Wikipedia method] [45] T F [clear acc] A 10 @ [t := a] T 14 @ A 12 @ [a := c;] T 10 @ S 14 @ [c := -t] T 12 @ A 11 @ [t := b] T 14 @ A 13 @ [b := d] T 11 @ S 14 @ [d := -t] T 13 @ A 8 @ [t := n + t] A 14 @ T 14 @ [Inner loop, get t div b by repeated subtraction] [61] A 14 @ [t := t - b] S 11 @ G 72 @ [jump out when t < 0] T 14 @ A 12 @ [c := c + a] A 10 @ T 12 @ A 13 @ [d := d + b] A 11 @ T 13 @ E 61 @ [loop back (always, since acc = 0)] [End of inner loop, print c/d preceded by space] [72] O 4 @ T F [74] A 74 @ [for subroutine return] G 15 @ [call subroutine to print c/d] A 1 @ [form 1 - d, to test for d = 1] S 13 @ G 45 @ [if d > 1, loop for next term] O 5 @ [else print end of line (CR LF)] O 6 @ [Next Farey series.] A 8 @ [load order] A 1 @ [add 1] E 33 @ [loop back] [Here when finished] [84] O 7 @ [output null to flush teleprinter buffer] Z F [stop] E 27 Z [define start of execution] P F [start with accumulator = 0]