+
    iZ:                         ^ RI HtHt ^ RIHt ^ RIHtHtHtH	t
 ^ RIHt ^ RIHt ^ RIHt  ! R R4      t ! R	 R
4      tR tR tR tR tR tR tRR ltRR ltR# )    )explog)_randint)	bit_scan1gcdinvertsqrt)_perfect_power)isprime)_sqrt_mod_prime_powerc                   2   a  ] tR t^	t o R tR tR tRtV tR# )SievePolynomialc                    Wn         W n        V^,          V n        ^V,          V,          V n        V^,          V,
          V n        R# )zThis class denotes the sieve polynomial.
Provide methods to compute `(a*x + b)**2 - N` and
`a*x + b` when given `x`.

Parameters
==========

a : parameter of the sieve polynomial
b : parameter of the sieve polynomial
N : number to be factored

N)aba2abb2)selfr   r   Ns   &&&&p/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/ntheory/qs.py__init__SievePolynomial.__init__
   s7     Q$A#a%Q$(    c                J    V P                   V,          V P                  ,           # N)r   r   r   xs   &&r   eval_uSievePolynomial.eval_u   s    vvax$&&  r   c                z    V P                   V,          V P                  ,           V,          V P                  ,           # r   )r   r   r   r   s   &&r   eval_vSievePolynomial.eval_v    s'    	DGG#Q&00r   )r   r   r   r   r   N)	__name__
__module____qualname____firstlineno__r   r   r"   __static_attributes____classdictcell____classdict__s   @r   r   r   	   s     &!1 1r   r   c                   *   a  ] tR t^$t o RtR tRtV tR# )FactorBaseElemz7This class stores an element of the `factor_base`.
    c                T    Wn         W n        W0n        RV n        RV n        RV n        R# )z
Initialization of factor_base_elem.

Parameters
==========

prime : prime number of the factor_base
tmem_p : Integer square root of x**2 = n mod prime
log_p : Compute Natural Logarithm of the prime
N)primetmem_plog_psoln1soln2b_ainv)r   r/   r0   r1   s   &&&&r   r   FactorBaseElem.__init__'   s*     

 

r   )r4   r1   r/   r2   r3   r0   N)r$   r%   r&   r'   __doc__r   r(   r)   r*   s   @r   r-   r-   $   s      r   r-   c                   ^ RI Hp . pRRrTVP                  ^V 4       F  p\        W^,
          ^,          V4      ^8X  g   K$  VR8  d   Vf   \	        V4      ^,
          pVR8  d   Vf   \	        V4      ^,
          p\        W^4      ^ ,          p\        \        V4      R,          4      pVP                  \        WgV4      4       K  	  WEV3# )a  Generate `factor_base` for Quadratic Sieve. The `factor_base`
consists of all the points whose ``legendre_symbol(n, p) == 1``
and ``p < num_primes``. Along with the prime `factor_base` also stores
natural logarithm of prime and the residue n modulo p.
It also returns the of primes numbers in the `factor_base` which are
close to 1000 and 5000.

Parameters
==========

prime_bound : upper prime bound of the factor_base
n : integer to be factored
)sieveNi  i     )
sympy.ntheory.generater8   
primerangepowlenr   roundr   appendr-   )	prime_boundnr8   factor_baseidx_1000idx_5000r/   residuer1   s	   &&       r   _generate_factor_baserF   <   s     -Kth!!![1q19"E*a/t| 0{+a/t| 0{+a/+Aa8;G#e*U*+E~eeDE 2 {**r   c              #    "   \        ^V ,          4      ^,          \        V4      ,
          pT;'       g    ^ pT;'       g    \        V4      ^,
          p RRRrp	\        ^24       F  p^p. p\        V4      V8  dH   ^ pV^ 8X  g   W9   d   V! Wx4      pK  W/,          P                  pVV,          pVP	                  V4       KW  \        \        V4      V,
          4      pVe*   \        V^,
          4      \        V^,
          4      8  g   K  Tp
Tp	TpK  	  T	pT
p. pV F~  pVV,          P                  pVV,          P                  \        VV,          V4      ,          V,          p^V,          V8  d
   VV,
          pVP	                  VV,          V,          4       K  	  \        V4      p\        VVV 4      pV F  pVVP                  ,          ^ 8X  d
   RVn        K$  \        VVP                  4      pV Uu. uF$  p^V,          V,          VP                  ,          NK&  	  upVn        VVP                  V,
          ,          VP                  ,          Vn        VVP                  ) V,
          ,          VP                  ,          Vn        K  	  Vx  \        ^^\        V4      ^,
          ,          4       EF  p\        V4      p^VV^,           ,	          ^,          ,          ^,
          pVP                  ^V,          VV,          ,          ,           pVP                   p\        VVV 4      pV F  pVP                  f   K  VP                  VVP                  V,          ,          ,
          VP                  ,          Vn        VP                  VVP                  V,          ,          ,
          VP                  ,          Vn        K  	  Vx  EK  	  EK}  u upi 5i)a  Generate sieve polynomials indefinitely.
Information such as `soln1` in the `factor_base` associated with
the polynomial is modified in place.

Parameters
==========

N : Number to be factored
M : sieve interval
factor_base : factor_base primes
idx_1000 : index of prime number in the factor_base near 1000
idx_5000 : index of prime number in the factor_base near to 5000
randint : A callable that takes two integers (a, b) and returns a random integer
          n such that a <= n <= b, similar to `random.randint`.
N)r   r=   ranger/   r?   r   absr0   r   sumr   r2   r4   r3   r   r   r   )r   MrB   rC   rD   randint
approx_valstartendbest_abest_q
best_ratio_r   qrand_ppratioBvalq_lgammar   gfba_invb_elemivneg_pows   &&&&&&                        r   _generate_polynomialrc   Y   s	      QqS!c!f$JMME

,
,s;'!+C
%)4
rAAAa&:%kV[$U0F'--Q A+,E!S^c*q.6I%I"
 " Cc"((C$++fQ#Xs.CCcIEw}eHHQVE\"  FAq!$B288|q 1bhh'EABCv6%"((22CBIryy1}-9BH		zA~."((:BH   q!c!fQh-(A!A!A,!+,q0Gai!n$AA1a(A!88#HHwryy|';;rxxGHHwryy|';;rxxG	 "
 G ) Ds    >OCOC,O7*O!F5Oc                   ^ .^V ,          ^,           ,          pV F  pVP                   f   K  \        WP                   ,           VP                  ,          ^V ,          VP                  4       F!  pW$;;,          VP                  ,          uu&   K#  	  VP                  ^8X  d   K  \        WP                  ,           VP                  ,          ^V ,          VP                  4       F!  pW$;;,          VP                  ,          uu&   K#  	  K  	  V# )a  Sieve Stage of the Quadratic Sieve. For every prime in the factor_base
that does not divide the coefficient `a` we add log_p over the sieve_array
such that ``-M <= soln1 + i*p <=  M`` and ``-M <= soln2 + i*p <=  M`` where `i`
is an integer. When p = 2 then log_p is only added using
``-M <= soln1 + i*p <=  M``.

Parameters
==========

M : sieve interval
factor_base : factor_base primes
)r2   rH   r/   r1   r3   )rK   rB   sieve_arrayfactoridxs   &&   r   _gen_sieve_arrayrh      s     #qsQw-K<<!ll*fll:AaCNC, O<<1!ll*fll:AaCNC, O  r   c                b   V ^ 8  d   V R,          p ^pM^ p\        V^4       F  w  r4WP                  ,          '       d   K  ^pWP                  ,          p WP                  ,          ^ 8X  d   V^,          pWP                  ,          p K4  V^,          '       g   Kx  V^V,          ,          pK  	  W 3# )zCheck if `num` is smooth with respect to the given `factor_base`
and compute its factorization vector.

Parameters
==========

num : integer whose smootheness is to be checked
factor_base : factor_base primes
)	enumerater/   )numrB   vecr`   r]   es   &&    r   _check_smoothnessro      s     Qwr	;*>>HHn!FAHHCq5516MC + 8Or   c                
   \        V4      \        V 4      ^,          ,           V,
          R,          p. p\        4       p	^VR,          P                  ,          p
\        W1) 4       EF  w  rW8  d   K  VP	                  V4      p\        W4      w  rV^8X  d%   VP                  VP                  V4      W34       KW  W8  g   K_  \        V4      '       g   Kr  W,          ^ 8X  d   V	P                  V4       K  VP                  V4      pW9   dl   VP                  V4      w  pppVV,          \        W4      ,          V ,          pVV,          V^,          ,          pVV,          pVP                  VW34       EK  VW3W_&   EK  	  W3# )a  Trial division stage. Here we trial divide the values generetated
by sieve_poly in the sieve interval and if it is a smooth number then
it is stored in `smooth_relations`. Moreover, if we find two partial relations
with same large prime then they are combined to form a smooth relation.
First we iterate over sieve array and look for values which are greater
than accumulated_val, as these values have a high chance of being smooth
number. Then using these values we find smooth relations.
In general, let ``t**2 = u*p modN`` and ``r**2 = v*p modN`` be two partial relations
with the same large prime p. Then they can be combined ``(t*r/p)**2 = u*v modN``
to form a smooth relation.

Parameters
==========

N : Number to be factored
M : sieve interval
factor_base : factor_base primes
sieve_array : stores log_p values
sieve_poly : polynomial from which we find smooth relations
partial_relations : stores partial relations with one large prime
ERROR_TERM : error term for accumulated_val
r9   rj   )r   setr/   rk   r"   ro   r?   r   r   addpopr   )r   rK   rB   re   
sieve_polypartial_relations
ERROR_TERMaccumulated_valsmooth_relationsproper_factorpartial_relation_upper_boundr   rY   ra   rm   rl   uu_prevv_prevvec_prevs   &&&&&&&             r   _trial_division_stager      sT   . 1vAq(:5>OEM#&{2'<'<#< K, a $Q4!8##Z%6%6q%91$BC/GCLLw!|!!#&!!!$A'+<+@+@+E(fHVC^+a/fHQ&x ''A4*+Q!&' -( **r   c              #  ,  "   V Uu. uF  q3^,          NK  	  pp\        V4      pR.V,          p\        V4       F  p^V,          p\        V4       Fy  p	WI,          V,          ;p
'       g   K  WV	,          ,          pWV	&   RWi&   \        V	^,           V4       F.  pWL,          V,          '       g   K  WL;;,          V,          uu&   K0  	   K  	  K  	  \        WdV4       F  w  rpV'       d   K  V^ ,          V^,          pp\        WdV4       FB  w  pppV'       g   K  VV,          '       g   K"  VV^ ,          ,          pVV^,          ,          pKD  	  \        V4      p^\	        VV,
          V 4      ;pu;8  d
   V 8  g   K  M K  Vx  K  	  R# u upi 5i)ao  Finds proper factor of N using fast gaussian reduction for modulo 2 matrix.

Parameters
==========

N : Number to be factored
smooth_relations : Smooth relations vectors matrix
col : Number of columns in the matrix

Reference
==========

.. [1] A fast algorithm for gaussian elimination over GF(2) and
its implementation on the GAPP. Cetin K.Koc, Sarath N.Arachchige
FTN)r=   rH   zipisqrtr   )r   rx   col
s_relationmatrixrowmarkposmr`   rV   add_coljmatrelr{   ra   m1mat1rel1r\   s   &&&                  r   _find_factorr     s\      /??.>
mm.>F?
f+C7S=DSzHsAIM!q!!Qi-q	q1uc*Ay1}}	W,	 +    4)9:1vs1v1!$0@ANBdrcDjjT!WT!W B
 !HSQ]"'a''G ; @s0   FFAF-?F1A'FF.AFFc           	     .    \        \        WW#V4      4      # )a  Performs factorization using Self-Initializing Quadratic Sieve.
In SIQS, let N be a number to be factored, and this N should not be a
perfect power. If we find two integers such that ``X**2 = Y**2 modN`` and
``X != +-Y modN``, then `gcd(X + Y, N)` will reveal a proper factor of N.
In order to find these integers X and Y we try to find relations of form
t**2 = u modN where u is a product of small primes. If we have enough of
these relations then we can form ``(t1*t2...ti)**2 = u1*u2...ui modN`` such that
the right hand side is a square, thus we found a relation of ``X**2 = Y**2 modN``.

Here, several optimizations are done like using multiple polynomials for
sieving, fast changing between polynomials and using partial relations.
The use of partial relations can speeds up the factoring by 2 times.

Parameters
==========

N : Number to be Factored
prime_bound : upper bound for primes in the factor base
M : Sieve Interval
ERROR_TERM : Error term for checking smoothness
seed : seed of random number generator

Returns
=======

set(int) : A set of factors of N without considering multiplicity.
           Returns ``{N}`` if factorization fails.

Examples
========

>>> from sympy.ntheory import qs
>>> qs(25645121643901801, 2000, 10000)
{5394769, 4753701529}
>>> qs(9804659461513846513, 2000, 10000)
{4641991, 2112166839943}

See Also
========

qs_factor

References
==========

.. [1] https://pdfs.semanticscholar.org/5c52/8a975c1405bd35c65993abf5a4edb667c1db.pdf
.. [2] https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve
)rq   	qs_factor)r   r@   rK   rv   seeds   &&&&&r   qsr   :  s    b y=>>r   c           
        V ^8  d   \        R4      h/ p. p/ pV ^,          ^ 8X  d2   ^pV ^,          p V ^,          ^ 8X  d   V ^,          p V^,          pK"  W^&   \        V 4      '       d   ^WP&   V# \        V ^4      ;p	'       d   V	w  rWV
&   V# T p\        V4      p\	        W4      w  rp\        V4      ^i,          ^d,          p\        WWW4       F  p\        W/4      p\        WVVVWs4      w  ppVV,          pV FE  pVV,          '       d   K  ^pVV,          pVV,          ^ 8X  d   VV,          pV^,          pK"  WV&   KG  	  V\        V4      8:  g   K   M	  \        W\        V4      ^,           4       F^  pVV,          ^ 8X  g   K  ^pVV,          pVV,          ^ 8X  d   VV,          pV^,          pK"  WV&   V^8X  g   \        V4      '       g   K^   M	  V^8w  d   ^W[&   V# )aU  Performs factorization using Self-Initializing Quadratic Sieve.

Parameters
==========

N : Number to be Factored
prime_bound : upper bound for primes in the factor base
M : Sieve Interval
ERROR_TERM : Error term for checking smoothness
seed : seed of random number generator

Returns
=======

dict[int, int] : Factors of N.
                 Returns ``{N: 1}`` if factorization fails.
                 Note that the key is not always a prime number.

Examples
========

>>> from sympy.ntheory import qs_factor
>>> qs_factor(1009 * 100003, 2000, 10000)
{1009: 1, 100003: 1}

See Also
========

qs

zN should be greater than 1)

ValueErrorr   r
   r   rF   r=   rc   rh   r   r   )r   r@   rK   rv   r   factorsrx   ru   rn   resultrA   N_copyrL   rC   rD   rB   	thresholdr\   re   s_relp_frV   rf   s   &&&&&                  r   r   r   n  s   @ 	1u566G 	1uz	a!eqj!GAFA
qzz
1%%v%
FtnG&;K&K#HK 3&+I!!xQ&q6*1k1N_l
sE!AzzAqLF1*/1QAJ  ,-- R  qC4Dq4HIF?aAvF6/Q&6!QFO{gfoo J {Nr   N)   i  )mathr   r   sympy.core.randomr   sympy.external.gmpyr   r   r   r	   r   sympy.ntheory.factor_r
   sympy.ntheory.primetestr   sympy.ntheory.residue_ntheoryr   r   r-   rF   rc   rh   ro   r   r   r   r    r   r   <module>r      s\     & E E 0 + ?1 16 0+:HV68/+d*Z1?hUr   