bridge/class-master/depend/pari/src/basemath/ifactor1.c
/* The following is invoked to walk back along the ambiguous cycle* until we
 * hit an ambiguous form and thus the desired factor, which it returns.  If it
 * fails for any reason, it returns 0.  It doesn't interfere with timing and
 * diagnostics, which it leaves to squfof().
 *
 * Before we invoke this, we've found a form (A, B, -C) with A = a^2, where a
 * isn't blacklisted and where gcd(a, B) = 1.  According to ACiCANT, we should
 * now proceed reducing the form (a, -B, -aC), but it is easy to show that the
 * first reduction step always sends this to (-aC, B, a), and the next one,
 * with q computed as usual from B and a (occupying the c position), gives a
 * reduced form, whose third member is easiest to recover by going back to D.
 * From this point onwards, we're once again working with single-word numbers.
 * No need to track signs, just work with the abs values of the coefficients. */
The following is invoked to walk back along the ambiguous cycle* until we
hit an ambiguous form and thus the desired factor, which it returns.  If it
fails for any reason, it returns 0.  It doesn't interfere with timing and
diagnostics, which it leaves to squfof().

Before we invoke this, we've found a form (A, B, -C) with A = a^2, where a
isn't blacklisted and where gcd(a, B) = 1.  According to ACiCANT, we should
now proceed reducing the form (a, -B, -aC), but it is easy to show that the
first reduction step always sends this to (-aC, B, a), and the next one,
with q computed as usual from B and a (occupying the c position), gives a
reduced form, whose third member is easiest to recover by going back to D.
From this point onwards, we're once again working with single-word numbers.
No need to track signs, just work with the abs values of the coefficients.
The following is invoked to walk back along the ambiguous cycle * until we hit an ambiguous form and thus the desired factor which it returns . If it fails for any reason it returns 0 . It doesn ' t interfere with timing and diagnostics which it leaves to squfof () . Before we invoke this we ' ve found a form ( A B - C ) with A = a^2 where a isn ' t blacklisted and where gcd ( a B ) = 1 . According to ACiCANT we should now proceed reducing the form ( a - B - aC ) but it is easy to show that the first reduction step always sends this to ( - aC B a ) and the next one with q computed as usual from B and a ( occupying the c position ) gives a reduced form whose third member is easiest to recover by going back to D . From this point onwards we ' re once again working with single - word numbers . No need to track signs just work with the abs values of the coefficients .
static long
squfof_ambig(long a, long B, long dd, GEN D)
{
  long b, c, q, qa, qc, qcb, a0, b0, b1, c0;
  long cnt = 0; 
  q = (dd + (B>>1)) / a;
  qa = q * a;
  b = (qa - B) + qa; 
  {
    pari_sp av = avma;
    c = itos(divis(shifti(subii(D, sqrs(b)), -2), a));
    avma = av;
  }
#ifdef DEBUG_SQUFOF
  err_printf("SQUFOF: ambigous cycle of discriminant %Ps\n", D);
  err_printf("SQUFOF: Form on ambigous cycle (%ld, %ld, %ld)\n", a, b, c);
#endif
  a0 = a; b0 = b1 = b;        
  for (;;) 
  { 
    c0 = c;
    if (c0 > dd)
      q = 1;
    else
      q = (dd + (b>>1)) / c0;
    if (q == 1)
    {
      qcb = c0 - b; b = c0 + qcb; c = a - qcb;
    }
    else
    {
      qc = q*c0; qcb = qc - b; b = qc + qcb; c = a - q*qcb;
    }
    a = c0;
    cnt++; if (b == b1) break;
    if (b == b0 && a == a0) return 0;
    b1 = b;
  }
  q = a&1 ? a : a>>1;
  if (DEBUGLEVEL >= 4)
  {
    if (q > 1)
      err_printf("SQUFOF: found factor %ld from ambiguous form\n"
                 "\tafter %ld steps on the ambiguous cycle\n",
                 q / ugcd(q,15), cnt);
    else
      err_printf("SQUFOF: ...found nothing on the ambiguous cycle\n"
                 "\tafter %ld steps there\n", cnt);
    if (DEBUGLEVEL >= 6) err_printf("SQUFOF: squfof_ambig returned %ld\n", q);
  }
  return q;
}
static long squfof_ambig ( long a , long B , long dd , GEN D ) { long b , c , q , qa , qc , qcb , a0 , b0 , b1 , c0 ; long cnt = 0 ; q = ( dd + ( B >> 1 ) ) / a ; qa = q * a ; b = ( qa - B ) + qa ; { pari_sp av = avma ; c = itos ( divis ( shifti ( subii ( D , sqrs ( b ) ) , -2 ) , a ) ) ; avma = av ; } #ifdef DEBUG_SQUFOF err_printf ( " \n " , D ) ; err_printf ( " \n " , a , b , c ) ; #endif a0 = a ; b0 = b1 = b ; for ( ; ; ) { c0 = c ; if ( c0 > dd ) q = 1 ; else q = ( dd + ( b >> 1 ) ) / c0 ; if ( q == 1 ) { qcb = c0 - b ; b = c0 + qcb ; c = a - qcb ; } else { qc = q * c0 ; qcb = qc - b ; b = qc + qcb ; c = a - q * qcb ; } a = c0 ; cnt ++ ; if ( b == b1 ) break ; if ( b == b0 && a == a0 ) return 0 ; b1 = b ; } q = a & 1 ? a : a >> 1 ; if ( DEBUGLEVEL >= 4 ) { if ( q > 1 ) err_printf ( " \n " " \t \n " , q / ugcd ( q , 15 ) , cnt ) ; else err_printf ( " \n " " \t \n " , cnt ) ; if ( DEBUGLEVEL >= 6 ) err_printf ( " \n " , q ) ; } return q ; }
The following is invoked to walk back along the ambiguous cycle* until we
 hit an ambiguous form and thus the desired factor, which it returns.
The following is invoked to walk back along the ambiguous cycle * until we hit an ambiguous form and thus the desired factor which it returns .
/* count reduction steps on the cycle */ /* avoid overflow */ /* end of loop detection and safeguard */ /* reduced cycles are finite */ /* reduction step */ /* safeguard against infinite loop: recognize when we've walked the entire
     * cycle in vain. (I don't think this can actually happen -- exercise.) */