Factors of an integer: Difference between revisions

Added Factors of an integer for EDSAC
(Added Factors of an integer for EDSAC)
Line 1,549:
→ (394ms 7776)
</lang>
 
=={{header|EDSAC order code}}==
Input is limited to 10 decimal digits, which is as many as the EDSAC print subroutine P7 can handle. Factors are printed in pairs, such that the product of the factors in each pair equals the input number.
<lang edsac>
[Factors of an integer, for Rosetta Code website.
EDSAC program, Initial Orders 2.]
 
[Modified library subroutine P7.
Prints signed integer; up to 10 digits, left-justified.
Input : 0D = integer
54 locations. Load at even address. Workspace 4D.]
T 56 K
GKA3FT42@A49@T31@ADE10@T31@A48@T31@SDTDH44#@NDYFLDT4DS43@
TFH17@S17@A43@G23@UFS43@T1FV4DAFG50@SFLDUFXFOFFFSFL4FT4DA49@
T31@A1FA43@G20@XFP1024FP610D@524D!FO46@O26@XFSFL8FT4DE39@
 
[Division subroutine for positive long integers.
35-bit dividend and divisor (max 2^34 - 1)
returning quotient and remainder.
Input: dividend at 4D, divisor at 6D
Output: remainder at 4D, quotient at 6D.
37 locations; working locations 0D, 8D.]
T 110 K
GKA3FT35@A6DU8DTDA4DRDSDG13@T36@ADLDE4@T36@T6DA4DSDG23@
T4DA6DYFYFT6DT36@A8DSDE35@T36@ADRDTDA6DLDT6DE15@EFPF
 
[********************** ROSETTA CODE TASK **********************
Subroutine to find and print factors of a positive integer.
Input: 0D = integer, maximum 10 decimal digits.
Load at even address.]
T 148 K
G K
A 3 F [form and plant link for return]
T 55 @
A D [load integer from 0D]
T 56#@ [store, since 0D is overwritten below]
A 62#@ [load 1]
T 58#@ [possible factor := 1]
S 65 @ [negative count of items per line]
T 64 @ [initialize count]
 
[Start of loop round possible factors]
[8] T F [clear acc]
A 56#@ [load integer]
T 4 D [to 4F for division]
A 58#@ [load possible factor]
T 6 D [to 6F for division]
A 13 @ [for return from next]
G 110 F [call division subroutine; clears acc]
A 6 D [save quotient (6D may be overwritten below)]
T 60#@
S 4 D [load negative of remainder]
G 44 @ [skip if remainder > 0]
 
[Here if m is a factor of n.
Print factor m and quotient n/m together]
T F [clear acc]
A 64 @ [test count of items per line]
G 26 @ [skip if not start of line]
S 65 @ [start of line, reset count]
T 64 @
O 70 @ [and print CR, LF]
O 71 @
[26] T F [clear acc]
O 67 @ [print '(']
A 58#@ [load factor]
T D [to 0D for printing]
A 30 @ [for return from next]
G 56 F [print factor; clears acc]
O 69 @ [print comma]
A 60#@ [load quotient]
T D [to 0D for printing]
A 35 @ [for return from next]
G 56 F [print quotient; clears acc]
O 68 @ [print ')']
A 64 @ [negative counter for items per line]
A 2 F [inc]
E 43 @ [skip if end of line]
O 66 @ [not end of line, print 2 spaces]
O 66 @
[43] T 64 @ [update counter]
 
[Common code after testing possible factor]
[44] T F [clear acc]
A 58#@ [load possible factor]
A 62#@ [inc by 1]
U 58#@ [store back]
S 60#@ [compare with quotient]
G 8 @ [loop if (new factor) < (old quotient)]
 
[Here when found all factors]
O 70 @ [print CR, LF twice]
O 71 @
O 70 @
O 71 @
T F [EDSAc convention is acc = 0 on return]
[55] E F [return]
[--------]
[56] P F P F [number whose factors are to be found]
[58] P F P F [possible factor]
[60] P F P F [integer part of (number/factor)]
[62] P D P F [1]
[64] P F [negative counter for items per line]
[65] P 4 F [items per line, in address field]
[66] ! F [space]
[67] K F [left parenthesis]
[68] L F [right parenthesis]
[69] N F [comma]
[70] @ F [carriage return]
[71] & F [line feed]
 
[Main routine for testing subroutine.
Load at even address.]
T 400 K
G K
[Test values. Not all are used below. Some have to be
stored negated; see EDSAC solution to Babbage problem]
[0] P 210 F P F [420]
[2] J 520 F P F [42,000]
[4] L 944 F V 2047 F [-420,000]
[6] & 847 D P F [99,999]
[8] # 1760 D V 2046 F [-999,999]
[10] D 768 D V 140 D [-999,999,999]
[12] # F [figure shift]
[13] K 4096 F [null character]
 
[Enter with acc = 0]
[14] O 12 @ [set teleprinter to figures]
A #@ [load 420]
T D [to 0D for subroutine]
A 17 @ [for return]
G 148 F [call subrouine]
S 10#@ [load 999,999,999 as negative of -999,999,999]
T D [to subroutine, as above]
A 21 @
G 148 F
O 13 @ [print null to flush printer buffer]
Z F [stop]
E 14 Z [define entry point]
P F [acc = 0 on entry]
</lang>
{{out}}
<pre>
(1,420) (2,210) (3,140) (4,105)
(5,84) (6,70) (7,60) (10,42)
(12,35) (14,30) (15,28) (20,21)
 
(1,999999999) (3,333333333) (9,111111111) (27,37037037)
(37,27027027) (81,12345679) (111,9009009) (333,3003003)
(999,1001001) (2997,333667)
</pre>
 
=={{header|Ela}}==
113

edits