
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


begin
    % return the factors of n ( n should be >= 1 ) in the array factor       %
    % the bounds of factor should be 0 :: len (len must be at least 1)       %
    % the number of factors will be returned in factor( 0 )                  %
    procedure getFactorsOf ( integer value n
                           ; integer array factor( * )
                           ; integer value len
                           ) ;
    begin
        for i := 0 until len do factor( i ) := 0;
        if n >= 1 and len >= 1 then begin
            integer pos, lastFactor;
            factor( 0 ) := factor( 1 ) := pos := 1;
            % find the factors up to sqrt( n )                               %
            for f := 2 until truncate( sqrt( n ) ) + 1 do begin
                if ( n rem f ) = 0 and pos <= len then begin
                    % found another factor and there's room to store it      %
                    pos           := pos + 1;
                    factor( 0   ) := pos;
                    factor( pos ) := f
                end if_found_factor
            end for_f;
            % find the factors above sqrt( n )                               %
            lastFactor := factor( factor( 0 ) );
            for f := factor( 0 ) step -1 until 1 do begin
                integer newFactor;
                newFactor := n div factor( f );
                if newFactor > lastFactor and pos <= len then begin
                    % found another factor and there's room to store it      %
                    pos           := pos + 1;
                    factor( 0   ) := pos;
                    factor( pos ) := newFactor
                end if_found_factor
            end for_f;
        end if_params_ok
    end getFactorsOf ;
 
 
    % prpocedure to test getFactorsOf                                        %
    procedure testFactorsOf( integer value n ) ;
    begin
        integer array factor( 0 :: 100 );
        getFactorsOf( n, factor, 100 );
        i_w := 1; s_w := 0; % set output format                              %
        write( n, " has ", factor( 0 ), " factors:" );
        for f := 1 until factor( 0 ) do writeon( " ", factor( f ) )
    end testFactorsOf ;
 
    % test the factorising                                                   %
    for i := 1 until 100 do testFactorsOf( i )
 
end.