view scripts/specfun/factor.m @ 31395:068342cc93b8

factor.m: Speed up for large inputs. __isprimelarge.cc__: Add implementation of Pollard Rho technique. New local functions localgcd, pollardrho, and new DEFUN __pollardrho__. factor.m: Call __pollardrho__ for inputs larger than 1e10. Change if-else ladder to favor faster technique for each value range. Remove long explanation of previous technique, now no longer applicable. Performance data: For inputs <= 1e10, performance is unchanged. For inputs around 1e12, it's about 33% faster, sampled over 1e4 inputs. For inputs around 1e15, it's about 13X faster, sampled over 1e4 inputs. For inputs around 1e18, it's about 400X faster, sampled over 100 inputs. For inputs beyond 2^63, it's 1000X faster, sampled over 100 inputs.
author Arun Giridhar <arungiridhar@gmail.com>
date Sat, 05 Nov 2022 00:10:11 -0400
parents bbb59cc6698c
children 0783418ac443
line wrap: on
line source

########################################################################
##
## Copyright (C) 2000-2022 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{pf} =} factor (@var{q})
## @deftypefnx {} {[@var{pf}, @var{n}] =} factor (@var{q})
## Return the prime factorization of @var{q}.
##
## The prime factorization is defined as @code{prod (@var{pf}) == @var{q}}
## where every element of @var{pf} is a prime number.  If @code{@var{q} == 1},
## return 1.  The output @var{pf} is of the same numeric class as the input.
##
## With two output arguments, return the unique prime factors @var{pf} and
## their multiplicities.  That is,
## @code{prod (@var{pf} .^ @var{n}) == @var{q}}.
##
## Implementation Note: The input @var{q} must be less than @code{flintmax}
## when the input is a floating-point class (double or single).
## @seealso{gcd, lcm, isprime, primes}
## @end deftypefn

function [pf, n] = factor (q)

  if (nargin < 1)
    print_usage ();
  endif

  if (! isscalar (q) || ! isreal (q) || q < 0 || q != fix (q))
    error ("factor: Q must be a real non-negative integer");
  endif

  ## Special case if q is prime, because isprime() is now much faster than
  ## factor().  This also absorbs the case of q < 4, where there are no primes
  ## less than sqrt(q).
  if (q < 4 || isprime (q))
    pf = q;
    n = 1;
    return;
  endif

  ## If we are here, then q is composite.

  cls = class (q);  # store class
  if (isfloat (q) && q > flintmax (q))
    error ("factor: Q too large to factor (> flintmax)");
  endif

  ## The overall flow is this:
  ## 1. Divide by small primes smaller than q^0.2, if any.
  ## 2. Use Pollard Rho to reduce the value below 1e10 if possible.
  ## 3. Divide by primes smaller than sqrt (q), if any.
  ## 4. At all stages, stop if the remaining value is prime.

  ## First divide by primes (q ^ 0.2).
  ## For q < 1e10, we can hard-code the primes.
  if (q < 1e10)
    smallprimes = feval (cls, ...
      [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97]);
  else
    smallprimes = primes (feval (cls, q ^ 0.2));
  endif

  ## pf is the list of prime factors returned with type of input class.
  pf = feval (cls, []);
  [pf, q] = reducefactors (q, pf, smallprimes);

  ## pf now contains all prime factors of q within smallprimes, including
  ## repetitions, in ascending order.
  ##
  ## q itself has been divided by those prime factors to become smaller,
  ## unless q was prime to begin with.

  sortflag = false;
  if (isprime (q))
    pf(end+1) = q;
  else
    ## Use Pollard Rho technique to pull factors one at a time.
    while (q > 1e10 && ! isprime (q))
      pr = feval (cls, __pollardrho__ (q));  # pr is a prime factor.
      [pf, q] = reducefactors (q, pf, pr);
      ## q is now divided by all occurrences of factor pr.
      sortflag = true;
    endwhile

    if (isprime (q))
      pf(end+1) = q;
    else
      ## If we are here, then q is composite but less than 1e10,
      ## and that is fast enough to test by division.
      largeprimes = primes (feval (cls, sqrt (q)));
      [pf, q] = reducefactors (q, pf, largeprimes);

      ## If q is still not 1, then it must be a prime of power 1.
      if (q > 1)
        pf(end+1) = q;
      endif
    endif
  endif

  ## The Pollard Rho technique can give factors in arbitrary order,
  ## so we need to sort pf if that was used.
  if (sortflag)
    pf = sort (pf);
  endif

  ## Determine multiplicity.
  if (nargout > 1)
    idx = find ([0, pf] != [pf, 0]);
    pf = pf(idx(1:length (idx)-1));
    n = diff (idx);
  endif

endfunction

function [pf, q] = reducefactors (qin, pfin, divisors)

  pf = pfin;
  q = qin;
  divisors = divisors (mod (q, divisors) == 0);

  for pp = divisors  # for each factor in turn
    ## Keep extracting all occurrences of that factor before going to larger
    ## factors.
    while (mod (q, pp) == 0)
      pf(end+1) = pp;
      q /= pp;
    endwhile
  endfor

endfunction


## Test special case input
%!assert (factor (1), 1)
%!assert (factor (2), 2)
%!assert (factor (3), 3)

%!test
%! for i = 2:20
%!   pf = factor (i);
%!   assert (prod (pf), i);
%!   assert (all (isprime (pf)));
%!   [pf, n] = factor (i);
%!   assert (prod (pf.^n), i);
%!   assert (all ([0,pf] != [pf,0]));
%! endfor

%!assert (factor (uint8 (8)), uint8 ([2 2 2]))
%!assert (factor (single (8)), single ([2 2 2]))
%!test
%! [pf, n] = factor (int16 (8));
%! assert (pf, int16 (2));
%! assert (n, double (3));

## Test input validation
%!error <Invalid call> factor ()
%!error <Q must be a real non-negative integer> factor ([1,2])
%!error <Q must be a real non-negative integer> factor (6i)
%!error <Q must be a real non-negative integer> factor (-20)
%!error <Q must be a real non-negative integer> factor (1.5)
%!error <Q too large to factor> factor (flintmax ("single") + 2)
%!error <Q too large to factor> factor (flintmax ("double") + 2)