Factors of an integer: Difference between revisions

Revised method of defining integers; noted changes in header.
(→‎Filter solution: Overhaul.)
(Revised method of defining integers; noted changes in header.)
Line 1,933:
=={{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.
 
2021-10-10 Integers are now read from the tape in decimal format, instead of being defined by the awkward method of pseudo-orders. The factorization of 999,999,999 has been removed, as it took too long on the commonly-used EdsacPC simulator (14.6 million orders - over 6 hours on the original EDSAC).
 
<lang edsac>
[Factors of an integer, forfrom Rosetta Code website.]
[EDSAC program, Initial Orders 2.]
 
[The numbers to be factorized are read in by library subroutine R2
(Wilkes, Wheeler and Gill, 1951 edition, pp.96-97, 148).]
[The address of the integers is placed in location 46, so they can be
referred to by the N parameter (or we could have used 45 and H, etc.)]
[12] # T F [figure46 shift]K
EP 600 14F Z [defineaddress entryof pointintegers]
[Subroutine R2]
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
T #N [pass address of integers to R2]
 
[List of integers to be factorized; edit ad lib. R2 requires 'F' after
each integer except the last, and '#' (pi) after the last.
This program uses 0 to mark the end of the list.]
42000F999999F0#
[6] & 847 DT P Z F [99,999resume normal loading]
 
[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 fromwhose 0Dfactors are to be found]
T 56#@ [store, since 0D is overwritten below]
A 62#@ [load 1]
T 58#@ [possible factor := 1]
Line 1,978 ⟶ 1,997:
T 6 D [to 6F for division]
A 13 @ [for return from next]
G 110 F [calldo division subroutine; clears acc]
A 6 D [save quotient (6D6F may be overwrittenchanged 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 the quotient n/m together]
T F [clear acc]
A 64 @ [test count of items per line]
Line 2,025 ⟶ 2,044:
O 70 @
O 71 @
T F [EDSAcexit convention iswith acc = 0 on return]
[55] E F [return]
[--------]
[56] PPF F P FPF [number whose factors are to be found]
[58] PPF F P FPF [possible factor]
[60] PPF F P FPF [integer part of (number/factor)]
[62] P D P FT62#Z PF [clear sandwich digit in 35-bit constant 1]
T 62 Z [resume normal loading]
[62] PD PF [35-bit constant 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 (in figures mode)]
[68] L F [right parenthesis (in figures mode)]
[69] N F [comma (in figures mode)]
[70] @ F [carriage return]
[71] & F [line feed]
 
[Main routine for testingdemonstrating subroutine.]
Load at even address.]
T 400 K
G K
[0] # [Test values. Not all areF used below. Some have[set tofigures bemode]
[131] K 4096 F [null characterchar]
stored negated; see EDSAC solution to Babbage problem]
[02] PS 210 F P#N [order to load negative of Ffirst [420number]
[23] JP 520 F 2 PF [to inc address by 2 Ffor [42,000next number]
[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]
[144] O 12 @ [set teleprinter to figures]
A 2 #@ [load 420order for first integer]
[6] T 7 D@ [toplant 0Din fornext subroutineorder]
[7] S A 17 @D [forload negative of 35-bit returninteger]
GE 148 F17 @ [callexit if number is subrouine0]
ST 10#@ [load 999,999,999 asD [negative ofto -999,999,9990D]
TS D [convert to subroutine, as abovepositive]
AT 21 @ D [pass to subroutine]
A 12 @ [call subroutine to find and print factors]
G 148 F
OA 13 7 @ [printmodify nullorder toabove, flushfor printernext bufferinteger]
[4] L 944 FA V 2047 F3 [-420,000]@
E 6 @ [always jump, since S = 12 > 0]
[--------]
[17] O 1 @ [done, print null to flush printer buffer]
Z F [stop]
 
E 14 Z [define entry point]
PE 4 Z F [accdefine =entry 0 on entrypoint]
P F [acc = 0 on entry]
</lang>
{{out}}
<pre>
(1,42042000) (2,21021000) (3,14014000) (4,10510500)
(5,848400) (6,707000) (7,606000) (108,425250)
(1210,354200) (1412,303500) (1514,283000) (2015,212800)
(16,2625) (20,2100) (21,2000) (24,1750)
(25,1680) (28,1500) (30,1400) (35,1200)
(40,1050) (42,1000) (48,875) (50,840)
(56,750) (60,700) (70,600) (75,560)
(80,525) (84,500) (100,420) (105,400)
(112,375) (120,350) (125,336) (140,300)
(150,280) (168,250) (175,240) (200,210)
 
(1,999999999999999) (3,333333333333333) (97,111111111142857) (279,37037037111111)
(3711,2702702790909) (8113,1234567976923) (11121,900900947619) (33327,300300337037)
(33,30303) (37,27027) (39,25641) (63,15873)
(999,1001001) (2997,333667)
(77,12987) (91,10989) (99,10101) (111,9009)
(117,8547) (143,6993) (189,5291) (231,4329)
(259,3861) (273,3663) (297,3367) (333,3003)
(351,2849) (407,2457) (429,2331) (481,2079)
(693,1443) (777,1287) (819,1221) (999,1001)
</pre>
 
113

edits