bridge/class-master/depend/pari/src/basemath/ifactor1.c
/* Split the first (composite) entry.  There _must_ already be room for another
 * factor below *where, and *where is updated. Two cases:
 * - entry = factor^k is a pure power: factor^k is inserted, leaving *where
 *   unchanged;
 * - entry = factor * cofactor (not necessarily coprime): both factors are
 *   inserted in the correct order, updating *where
 * The inserted factors class is set to unknown, they inherit the exponent
 * (or a multiple thereof) of their ancestor.
 *
 * Returns number of factors written into the structure, normally 2 (1 if pure
 * power, maybe > 2 if a factoring engine returned a vector of factors instead
 * of a single factor). Can reallocate the data structure in the
 * vector-of-factors case, not in the most common single-factor case.
 * Stack housekeeping:  this routine may create one or more objects  (a new
 * factor, or possibly several, and perhaps one or more new exponents > 2) */
Split the first (composite) entry.  There _must_ already be room for another
factor below *where, and *where is updated. Two cases:
- entry = factor^k is a pure power: factor^k is inserted, leaving *where
unchanged;
- entry = factor * cofactor (not necessarily coprime): both factors are
inserted in the correct order, updating *where
The inserted factors class is set to unknown, they inherit the exponent
(or a multiple thereof) of their ancestor.

Returns number of factors written into the structure, normally 2 (1 if pure
power, maybe > 2 if a factoring engine returned a vector of factors instead
of a single factor). Can reallocate the data structure in the
vector-of-factors case, not in the most common single-factor case.
Stack housekeeping:  this routine may create one or more objects  (a new
factor, or possibly several, and perhaps one or more new exponents > 2)
Split the first ( composite ) entry . There _must_ already be room for another factor below * where and * where is updated . Two cases : - entry = factor^k is a pure power : factor^k is inserted leaving * where unchanged ; - entry = factor * cofactor ( not necessarily coprime ) : both factors are inserted in the correct order updating * where The inserted factors class is set to unknown they inherit the exponent ( or a multiple thereof ) of their ancestor . Returns number of factors written into the structure normally 2 ( 1 if pure power maybe > 2 if a factoring engine returned a vector of factors instead of a single factor ) . Can reallocate the data structure in the vector - of - factors case not in the most common single - factor case . Stack housekeeping : this routine may create one or more objects ( a new factor or possibly several and perhaps one or more new exponents > 2 )
static long
ifac_crack(GEN *partial, GEN *where, long moebius_mode)
{
  long cmp_res, hint = get_hint(partial);
  GEN factor, exponent;
#ifdef IFAC_DEBUG
  ifac_check(*partial, *where);
  if (*where < *partial + 6)
    pari_err_BUG("ifac_crack ['*where' out of bounds]");
  if (!(VALUE(*where)) || typ(VALUE(*where)) != t_INT)
    pari_err_BUG("ifac_crack [incorrect VALUE(*where)]");
  if (CLASS(*where) != gen_0)
    pari_err_BUG("ifac_crack [operand not known composite]");
#endif
  if (DEBUGLEVEL>2) {
    err_printf("IFAC: cracking composite\n\t%Ps\n", **where);
    if (DEBUGLEVEL>3) err_printf("IFAC: checking for pure square\n");
  }
  {
    forprime_t T;
    ulong exp = 1, mask = 7;
    long good = 0;
    pari_sp av = avma;
    (void)u_forprime_init(&T, 11, ULONG_MAX);
    while (Z_issquareall(VALUE(*where), &factor))
    {
      good = 1; 
      update_pow(*where, factor, 2, &av);
      if (moebius_mode) return 0; 
    }
    while ( (exp = is_357_power(VALUE(*where), &factor, &mask)) )
    {
      good = 1; 
      update_pow(*where, factor, exp, &av);
      if (moebius_mode) return 0; 
    }
    while ( (exp = is_pth_power(VALUE(*where), &factor, &T, 15)) )
    {
      good = 1; 
      update_pow(*where, factor, exp, &av);
      if (moebius_mode) return 0; 
    }
    if (good && hint != 15 && ifac_checkprime(*where))
    { 
      if (DEBUGLEVEL>3)
        err_printf("IFAC: factor %Ps\n\tis prime\n", VALUE(*where));
      return 0; 
    }
  } 
  factor = NULL;
  if (!(hint & 4))
  { 
    if (DEBUGLEVEL >= 4) err_printf("IFAC: trying Pollard-Brent rho method\n");
    factor = pollardbrent(VALUE(*where));
    if (!factor)
    { 
      if (DEBUGLEVEL >= 4)
        err_printf("IFAC: trying Shanks' SQUFOF, will fail silently if input\n"
                   "      is too large for it.\n");
      factor = squfof(VALUE(*where));
    }
  }
  if (!factor && !(hint & 2))
  { 
    if (DEBUGLEVEL >= 4) err_printf("IFAC: trying Lenstra-Montgomery ECM\n");
    factor = ellfacteur(VALUE(*where), 0); 
  }
  if (!factor && !(hint & 1))
  { 
    if (DEBUGLEVEL >= 4) err_printf("IFAC: trying MPQS\n");
    factor = mpqs(VALUE(*where));
  }
  if (!factor)
  {
    if (!(hint & 8))
    { 
      if (DEBUGLEVEL >= 4)
        err_printf("IFAC: forcing ECM, may take some time\n");
      factor = ellfacteur(VALUE(*where), 1);
    }
    else
    { 
      if (DEBUGLEVEL >= 2)
      {
        if (hint != 15)
          pari_warn(warner, "IFAC: unfactored composite declared prime");
        else
          pari_warn(warner, "IFAC: untested integer declared prime");
        if (DEBUGLEVEL == 2) err_printf("\t%Ps\n", VALUE(*where));
      }
      CLASS(*where) = gen_1; 
      return 1;
    }
  }
  if (typ(factor) == t_VEC) 
    return ifac_insert_multiplet(partial, where, factor, moebius_mode);
  if (!dvdiiz(VALUE(*where), factor, VALUE(*where)))
  {
    err_printf("IFAC: factoring %Ps\n", VALUE(*where));
    err_printf("\tyielded 'factor' %Ps\n\twhich isn't!\n", factor);
    pari_err_BUG("factoring");
  }
  if (DEBUGLEVEL >= 4) err_printf("IFAC: cofactor = %Ps\n", VALUE(*where));
  cmp_res = cmpii(factor, VALUE(*where));
  CLASS(*where) = NULL; 
  exponent = EXPON(*where);
  *where -= 3;
  CLASS(*where) = NULL; 
  EXPON(*where) = isonstack(exponent)? icopy(exponent): exponent;
  if (cmp_res < 0)
    VALUE(*where) = factor; 
  else if (cmp_res > 0)
  { 
    GEN old = *where + 3;
    VALUE(*where) = VALUE(old); 
    VALUE(old) = factor; 
  }
  else pari_err_BUG("ifac_crack [Z_issquareall miss]");
  return 2;
}
static long ifac_crack ( GEN * partial , GEN * where , long moebius_mode ) { long cmp_res , hint = get_hint ( partial ) ; GEN factor , exponent ; #ifdef IFAC_DEBUG ifac_check ( * partial , * where ) ; if ( * where < * partial + 6 ) pari_err_BUG ( " " ) ; if ( ! ( VALUE ( * where ) ) || typ ( VALUE ( * where ) ) != t_INT ) pari_err_BUG ( " " ) ; if ( CLASS ( * where ) != gen_0 ) pari_err_BUG ( " " ) ; #endif if ( DEBUGLEVEL > 2 ) { err_printf ( " \n \t \n " , * * where ) ; if ( DEBUGLEVEL > 3 ) err_printf ( " \n " ) ; } { forprime_t T ; ulong exp = 1 , mask = 7 ; long good = 0 ; pari_sp av = avma ; ( void ) u_forprime_init ( & T , 11 , ULONG_MAX ) ; while ( Z_issquareall ( VALUE ( * where ) , & factor ) ) { good = 1 ; update_pow ( * where , factor , 2 , & av ) ; if ( moebius_mode ) return 0 ; } while ( ( exp = is_357_power ( VALUE ( * where ) , & factor , & mask ) ) ) { good = 1 ; update_pow ( * where , factor , exp , & av ) ; if ( moebius_mode ) return 0 ; } while ( ( exp = is_pth_power ( VALUE ( * where ) , & factor , & T , 15 ) ) ) { good = 1 ; update_pow ( * where , factor , exp , & av ) ; if ( moebius_mode ) return 0 ; } if ( good && hint != 15 && ifac_checkprime ( * where ) ) { if ( DEBUGLEVEL > 3 ) err_printf ( " \n \t \n " , VALUE ( * where ) ) ; return 0 ; } } factor = NULL ; if ( ! ( hint & 4 ) ) { if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " ) ; factor = pollardbrent ( VALUE ( * where ) ) ; if ( ! factor ) { if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " " \n " ) ; factor = squfof ( VALUE ( * where ) ) ; } } if ( ! factor && ! ( hint & 2 ) ) { if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " ) ; factor = ellfacteur ( VALUE ( * where ) , 0 ) ; } if ( ! factor && ! ( hint & 1 ) ) { if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " ) ; factor = mpqs ( VALUE ( * where ) ) ; } if ( ! factor ) { if ( ! ( hint & 8 ) ) { if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " ) ; factor = ellfacteur ( VALUE ( * where ) , 1 ) ; } else { if ( DEBUGLEVEL >= 2 ) { if ( hint != 15 ) pari_warn ( warner , " " ) ; else pari_warn ( warner , " " ) ; if ( DEBUGLEVEL == 2 ) err_printf ( " \t \n " , VALUE ( * where ) ) ; } CLASS ( * where ) = gen_1 ; return 1 ; } } if ( typ ( factor ) == t_VEC ) return ifac_insert_multiplet ( partial , where , factor , moebius_mode ) ; if ( ! dvdiiz ( VALUE ( * where ) , factor , VALUE ( * where ) ) ) { err_printf ( " \n " , VALUE ( * where ) ) ; err_printf ( " \t \n \t \n " , factor ) ; pari_err_BUG ( " " ) ; } if ( DEBUGLEVEL >= 4 ) err_printf ( " \n " , VALUE ( * where ) ) ; cmp_res = cmpii ( factor , VALUE ( * where ) ) ; CLASS ( * where ) = NULL ; exponent = EXPON ( * where ) ; * where -= 3 ; CLASS ( * where ) = NULL ; EXPON ( * where ) = isonstack ( exponent ) ? icopy ( exponent ) : exponent ; if ( cmp_res < 0 ) VALUE ( * where ) = factor ; else if ( cmp_res > 0 ) { GEN old = * where + 3 ; VALUE ( * where ) = VALUE ( old ) ; VALUE ( old ) = factor ; } else pari_err_BUG ( " " ) ; return 2 ; }
Split the first (composite) entry.
Split the first ( composite ) entry .
/* MPQS cannot factor prime powers. Look for pure powers even if MPQS is
   * blocked by hint: fast and useful in bounded factorization */ /* crack squares */ /* remember we succeeded once */ /* no need to carry on */ /* remember we succeeded once */ /* no need to carry on */ /* cutoff at 14 bits as trial division must have found everything below */ /* remember we succeeded once */ /* no need to carry on */ /* our composite was a prime power */ /* bypass subsequent ifac_whoiswho() call */ /* pure power stage */ /* pollardbrent() Rho usually gets a first chance */ /* Shanks' squfof() */ /* First ECM stage */ /* do not insist */ /* MPQS stage */ /* still no luck? Final ECM stage, guaranteed to succeed */ /* limited factorization */ /* don't print it out at level 3 or above, where it would appear
         * several times before and after this message already */ /* might as well trial-divide by it... */ /* delegate this case */ /* typ(factor) == t_INT */ /* got single integer back:  work out the cofactor (in place) */ /* factoring engines report the factor found; tell about the cofactor */ /* The two factors are 'factor' and VALUE(*where), find out which is larger */ /* mark factor /cofactor 'unknown' */ /* mark factor /cofactor 'unknown' */ /* common case */ /* factor > cofactor, rearrange */ /* move cofactor pointer to lowest slot */ /* save factor */