
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses


Task

Compute the   factors   of a positive integer.

These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.

(Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases).

Note that every prime number has two factors:   1   and itself.



Related tasks

   count in factors
   prime decomposition
   Sieve of Eratosthenes
   primality by trial division
   factors of a Mersenne number
   trial factoring of a Mersenne number
   partition an integer X into N primes
   sequence of primes by Trial Division
   sequence: smallest number greater than previous term with exactly n divisors


 
  [Factors of an integer, from 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.)]
            T   46 K
            P  600 F  [address of integers]
[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#
            T      Z  [resume 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 whose factors are to be found]
            T   56#@ [store]
            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 [do division; clears acc]
            A    6 D [save quotient (6F may be changed 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 m and the quotient 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 [exit with acc = 0]
     [55]   E      F [return]
           [--------]
     [56]   PF    PF [number whose factors are to be found]
     [58]   PF    PF [possible factor]
     [60]   PF    PF [integer part of (number/factor)]
            T62#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 demonstrating subroutine.]
            T  400 K
            G      K
      [0]   #      F [set figures mode]
      [1]   K 4096 F [null char]
      [2]   S     #N [order to load negative of first number]
      [3]   P    2 F [to inc address by 2 for next number]
 
          [Enter with acc = 0]
      [4]   O      @ [set teleprinter to figures]
            A    2 @ [load order for first integer]
      [6]   T    7 @ [plant in next order]
      [7]   S      D [load negative of 35-bit integer]
            E   17 @ [exit if number is 0]
            T      D [negative to 0D]
            S      D [convert to positive]
            T      D [pass to subroutine]
            A   12 @ [call subroutine to find and print factors]
            G  148 F
            A    7 @ [modify order above, for next integer]
            A    3 @
            E    6 @ [always jump, since S = 12 > 0]
           [--------]
     [17]   O    1 @ [done, print null to flush printer buffer]
            Z      F [stop]
 
            E    4 Z  [define entry point]
            P      F  [acc = 0 on entry]
 