ó
    Š*£hWÔ  ã                  ó  • S SK Jr  S SKJrJrJrJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJrJrJrJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  S SKJr  S SKJr  S SK J!r!  S SK"J#r#  S SK$J%r%J&r&  S SK'J(r(  S r)S r*S r+S r,S9S jr-S r.S r/S:S jr0\14S jr2S r3S r4S r5S r6S r7S r8S  r9S:S! jr:S;S" jr;\" S#S$S%S&9S' 5       r<\" S(S$S%S&9S) 5       r=\" S*S$S%S&9S+ 5       r>S<S- jr?S<S. jr@S=S/ jrAS>S0 jrBS<S1 jrCS?S2 jrDS?S3 jrES4 rFS5 rGS6 rHS7 rIS8 rJg,)@é    )Úannotations)ÚgcdÚlcmÚinvertÚsqrtÚjacobiÚ	bit_scan1Úremove)ÚPoly)ÚZZ)Úgf_crt1Úgf_crt2Úlinear_congruenceÚ	gf_csolveé   )Úisprime)Ú
primerange)Ú	factorintÚ_perfect_power©Úcrt)Ú
deprecated)Úrecurrence_memo)Úas_int)Úiproduct)Ú_randintÚrandint)Úproductc                óü  • [        U 5      [        U5      pUS::  a  [        S5      eX-  n U S:X  a  g[        X5      S:w  a  [        S5      eSn[        U5      R	                  5        Hˆ  u  p4X4-  nUS-
  X4S-
  -  -  n[        US-
  5      nUS:”  a  US-
  Xs'   SnUR	                  5        H6  u  pš[        XXš-  -  U5      nUS:w  d  M  [        X¹U5      nX‰-  nUS:w  a  M  M8     [        X(5      nMŠ     [        U5      $ )a°  Returns the order of ``a`` modulo ``n``.

Explanation
===========

The order of ``a`` modulo ``n`` is the smallest integer
``k`` such that `a^k` leaves a remainder of 1 with ``n``.

Parameters
==========

a : integer
n : integer, n > 1. a and n should be relatively prime

Returns
=======

int : the order of ``a`` modulo ``n``

Raises
======

ValueError
    If `n \le 1` or `\gcd(a, n) \neq 1`.
    If ``a`` or ``n`` is not an integer.

Examples
========

>>> from sympy.ntheory import n_order
>>> n_order(3, 7)
6
>>> n_order(4, 7)
3

See Also
========

is_primitive_root
    We say that ``a`` is a primitive root of ``n``
    when the order of ``a`` modulo ``n`` equals ``totient(n)``

r   ú%n should be an integer greater than 1ú*The two numbers should be relatively prime)r   Ú
ValueErrorr   r   ÚitemsÚpowr   Úint)ÚaÚnÚa_orderÚpÚeÚpeÚpe_orderÚfactorsÚorderÚpxÚexÚxs               ÚZ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/ntheory/residue_ntheory.pyÚn_orderr3      s  € ôX �!‹9”f˜Q“i€qØˆAƒvÜÐ@ÓAÐAØ	‰€AàˆAƒvØÜ
ˆ1ƒy�Aƒ~ÜÐEÓFÐFØ€GÜ˜!“×"Ñ"Ö$‰ˆØ‰TˆØ˜‘E˜Q Q¡™ZÑ'ˆÜ˜A ™EÓ"ˆØˆq‹5Ø˜Q™ˆG‰JØˆØ—m‘m–o‰FˆBÜ�A 2¡6Ñ)¨2Ó.ˆAØ�q•&Ü˜˜r“N�Ø‘�ð �q—&ñ &ô
 �gÓ%Šñ %ô ˆw‹<Ðó    c              #  ó¨  ^ ^#   • T S:X  a  Sv •  gT S-  S;   a  SOSnT S:  a  T S:X  a  SOUmOb[        T S	-
  5      R                  5        Vs/ s H  nT S	-
  U-  PM     nn[        UT 5       H   m[        UU 4S
 jU 5       5      (       d  M     O   Tv •  [        ST S5       H'  n[	        T S	-
  U5      S	:X  d  M  [        TUT 5      v •  M)     gs  snf 7f)aœ  Generates the primitive roots for a prime ``p``.

Explanation
===========

The primitive roots generated are not necessarily sorted.
However, the first one is the smallest primitive root.

Find the element whose order is ``p-1`` from the smaller one.
If we can find the first primitive root ``g``, we can use the following theorem.

.. math ::
    \operatorname{ord}(g^k) = \frac{\operatorname{ord}(g)}{\gcd(\operatorname{ord}(g), k)}

From the assumption that `\operatorname{ord}(g)=p-1`,
it is a necessary and sufficient condition for
`\operatorname{ord}(g^k)=p-1` that `\gcd(p-1, k)=1`.

Parameters
==========

p : odd prime

Yields
======

int
    the primitive roots of ``p``

Examples
========

>>> from sympy.ntheory.residue_ntheory import _primitive_root_prime_iter
>>> sorted(_primitive_root_prime_iter(19))
[2, 3, 10, 13, 14, 15]

References
==========

.. [1] W. Stein "Elementary Number Theory" (2011), page 44

é   é   Né   )r   é   é)   é   é   r   c              3  óD   >#   • U  H  n[        TUT5      S :g  v •  M     g7f©r   N©r$   )Ú.0ÚpwÚgr)   s     €€r2   Ú	<genexpr>Ú-_primitive_root_prime_iter.<locals>.<genexpr>’   s   øé € Ð2²¨"”3�q˜"˜a“= AÖ%²ùs   ƒ )r   ÚkeysÚrangeÚallr   r$   )r)   Úg_minÚiÚvÚkrB   s   `    @r2   Ú_primitive_root_prime_iterrL   [   sÑ   ùé € ðV 	ˆAƒvØŠØð �Q‘˜&“‰A a€EØˆ2ƒvà�b“‰A˜e‰ä#,¨Q°©UÓ#3×#8Ñ#8Ô#:Ó;Ò#:˜aˆa�!‰e˜Œ\Ñ#:ˆÐ;Ü�u˜a–ˆAÜÕ2±Ó2×2Ó2Ùñ !ð ‚Gä�1�a˜Ž^ˆÜˆq�1‰u�a‹=˜AÕÜ�a˜˜A“,Ôò ùò <ùs   „ACÁCÁ*CÂ)CÂ9Cc              #  ó   ^^#   • US:X  a  [        U 5       Sh  v•N   gU S-  n[        U 5       HV  mT[        TSU -
  U5      -
  U-  n[        SX 5       H-  mTU:w  d  M  UU4S j[        SX-  U5       5        Sh  v•N   M/     MX     g Np N7f)a  Generates the primitive roots of `p^e`.

Explanation
===========

Let ``g`` be the primitive root of ``p``.
If `g^{p-1} \not\equiv 1 \pmod{p^2}`, then ``g`` is primitive root of `p^e`.
Thus, if we find a primitive root ``g`` of ``p``,
then `g, g+p, g+2p, \ldots, g+(p-1)p` are primitive roots of `p^2` except one.
That one satisfies `\hat{g}^{p-1} \equiv 1 \pmod{p^2}`.
If ``h`` is the primitive root of `p^2`,
then `h, h+p^2, h+2p^2, \ldots, h+(p^{e-2}-1)p^e` are primitive roots of `p^e`.

Parameters
==========

p : odd prime
e : positive integer

Yields
======

int
    the primitive roots of `p^e`

Examples
========

>>> from sympy.ntheory.residue_ntheory import _primitive_root_prime_power_iter
>>> sorted(_primitive_root_prime_power_iter(5, 2))
[2, 3, 8, 12, 13, 17, 22, 23]

r   Nr7   r   c              3  ó4   >#   • U  H  nTT-   U-   v •  M     g 7f©N© )r@   ÚmrB   rK   s     €€r2   rC   Ú3_primitive_root_prime_power_iter.<locals>.<genexpr>Å   s   øé € ÐFÒ3E¨a  A¡¨¦	Ò3Eùs   ƒ)rL   r$   rF   )r)   r*   Úp2ÚtrB   rK   s       @@r2   Ú _primitive_root_prime_power_iterrU   ›   s�   ùé € ðD 	ˆAƒvÜ-¨aÓ0×0Ñ0à�‰TˆÜ+¨AÖ.ˆAØ”S˜˜A ™E 2Ó&Ñ&¨"Ñ,ˆAÜ˜1˜b–_�Ø˜•6ÝF´5¸¸A¹DÀ"Ô3EÓF×FÒFó %ò /ñ 	1ñ Gùs(   „B™B
šABÁBÁ<BÁ=BÂBc              #  ó`   #   • [        X5       H  nUS-  S:X  a  Uv •  M  X U-  -   v •  M     g7f)aç  Generates the primitive roots of `2p^e`.

Explanation
===========

If ``g`` is the primitive root of ``p**e``,
then the odd one of ``g`` and ``g+p**e`` is the primitive root of ``2*p**e``.

Parameters
==========

p : odd prime
e : positive integer

Yields
======

int
    the primitive roots of `2p^e`

Examples
========

>>> from sympy.ntheory.residue_ntheory import _primitive_root_prime_power2_iter
>>> sorted(_primitive_root_prime_power2_iter(5, 2))
[3, 13, 17, 23, 27, 33, 37, 47]

r7   r   N)rU   )r)   r*   rB   s      r2   Ú!_primitive_root_prime_power2_iterrW   È   s1   é € ô: .¨aÖ3ˆØˆq‰5�A‹:ØŒGà˜‘d‘(ŒNò	 4ùs   ‚,.c                ó¾  • [        U 5      n U S::  a  [        S5      eU S::  a  U S-
  $ U S-  S:H  nU(       d  U nOU S-  (       a  U S-  nOg[        U5      (       a  SnO)[        US5      nU(       d  gUu  p4[        U5      (       d  gU(       d/  U(       a  [	        [        X45      5      $ [	        [        X45      5      $ U(       a3  [        SU S5       H"  nXc-  (       d  M  [        X`5      (       d  M   Us  $    [	        [        U5      5      nUS:X  d  [        XsS-
  US-  5      S:w  a  U$ [        US-   U 5       H"  nXc-  (       d  M  [        X`5      (       d  M   Us  $    g)a>  Returns a primitive root of ``p`` or None.

Explanation
===========

For the definition of primitive root,
see the explanation of ``is_primitive_root``.

The primitive root of ``p`` exist only for
`p = 2, 4, q^e, 2q^e` (``q`` is an odd prime).
Now, if we know the primitive root of ``q``,
we can calculate the primitive root of `q^e`,
and if we know the primitive root of `q^e`,
we can calculate the primitive root of `2q^e`.
When there is no need to find the smallest primitive root,
this property can be used to obtain a fast primitive root.
On the other hand, when we want the smallest primitive root,
we naively determine whether it is a primitive root or not.

Parameters
==========

p : integer, p > 1
smallest : if True the smallest primitive root is returned or None

Returns
=======

int | None :
    If the primitive root exists, return the primitive root of ``p``.
    If not, return None.

Raises
======

ValueError
    If `p \le 1` or ``p`` is not an integer.

Examples
========

>>> from sympy.ntheory.residue_ntheory import primitive_root
>>> primitive_root(19)
2
>>> primitive_root(21) is None
True
>>> primitive_root(50, smallest=False)
27

See Also
========

is_primitive_root

References
==========

.. [1] W. Stein "Elementary Number Theory" (2011), page 44
.. [2] P. Hackman "Elementary Number Theory" (2009), Chapter C

r   ú%p should be an integer greater than 1é   r7   r   Nr6   )r   r"   r   r   ÚnextrW   rU   rF   Úis_primitive_rootrL   r$   )r)   ÚsmallestÚp_evenÚqr*   rQ   rI   rB   s           r2   Úprimitive_rootr`   ì   s?  € ô| 	ˆq‹	€AØˆAƒvÜÐ@ÓAÐAØˆAƒvØ�1‰uˆØ�‰U�a‰Z€FÞØ‰Ø	
ˆQ�Øˆq‰D‰àÜˆq‡z�zØ‰ä˜1˜aÓ ˆÞØØ‰ˆÜ�q�z‰zØÞÞÜÔ9¸!Ó?Ó@Ð@ÜÔ4°QÓ:Ó;Ð;ÞÜ�q˜!˜Q–ˆAØ�u‰uÔ*¨1×0Ó0Ø’ñ  ô 	Ô'¨Ó*Ó+€AØˆAƒv”�Q˜A™˜q !™tÓ$¨Ó)ØˆÜ�1�q‘5˜!Ž_ˆØ�5‰5Ô& q×,Ó,ØŠHò r4   c                óZ  ^ ^^• [        T 5      [        T5      sm mTS::  a  [        S5      eT T-  m [        T T5      S:w  a  [        S5      eTS::  a  T TS-
  :H  $ TS-  (       a  TnOTS-  (       a  TS-  nOg[        U5      (       a"  US-
  m[	        US-
  5      R                  5       nOl[        US5      nU(       d  gUu  p%[        U5      (       d  gX%S-
  -  US-
  -  m[        [	        US-
  5      R                  5       5      nUR                  U5        [        U UU4S jU 5       5      $ )	aš  Returns True if ``a`` is a primitive root of ``p``.

Explanation
===========

``a`` is said to be the primitive root of ``p`` if `\gcd(a, p) = 1` and
`\phi(p)` is the smallest positive number s.t.

    `a^{\phi(p)} \equiv 1 \pmod{p}`.

where `\phi(p)` is Euler's totient function.

The primitive root of ``p`` exist only for
`p = 2, 4, q^e, 2q^e` (``q`` is an odd prime).
Hence, if it is not such a ``p``, it returns False.
To determine the primitive root, we need to know
the prime factorization of ``q-1``.
The hardness of the determination depends on this complexity.

Parameters
==========

a : integer
p : integer, ``p`` > 1. ``a`` and ``p`` should be relatively prime

Returns
=======

bool : If True, ``a`` is the primitive root of ``p``.

Raises
======

ValueError
    If `p \le 1` or `\gcd(a, p) \neq 1`.
    If ``a`` or ``p`` is not an integer.

Examples
========

>>> from sympy.functions.combinatorial.numbers import totient
>>> from sympy.ntheory import is_primitive_root, n_order
>>> is_primitive_root(3, 10)
True
>>> is_primitive_root(9, 10)
False
>>> n_order(3, 10) == totient(10)
True
>>> n_order(9, 10) == totient(10)
False

See Also
========

primitive_root

r   rY   r!   rZ   r7   Fr6   c              3  óJ   >#   • U  H  n[        TTU-  T5      S :g  v •  M     g7fr>   r?   )r@   Úprimer&   Úgroup_orderr)   s     €€€r2   rC   Ú$is_primitive_root.<locals>.<genexpr>§  s$   øé € ÐIÂ¸Œs�1�k UÑ*¨AÓ.°!Ö3Âùs   ƒ #)
r   r"   r   r   r   rE   r   ÚsetÚaddrG   )r&   r)   r_   r-   rQ   r*   rd   s   ``    @r2   r\   r\   O  s  ú€ ôt �!‹9”f˜Q“i€D€A€qØˆAƒvÜÐ@ÓAÐAØ	ˆA‰€AÜ
ˆ1ˆaƒy�Aƒ~ÜÐEÓFÐFð 	ˆAƒvà�A˜‘E‰zÐØˆ1‡uØ‰Ø	
ˆQ�Øˆq‰D‰àÜˆq‡z�zØ˜!‘eˆÜ˜A ™EÓ"×'Ñ'Ó)‰ä˜1˜aÓ ˆÞØØ‰ˆÜ�q�z‰zØØ˜a™%‘j ! a¡%Ñ(ˆÜ”i  A¡Ó&×+Ñ+Ó-Ó.ˆØ�‰�AŒÜÖIÁÓIÓIÐIr4   c                óº  • [        US-
  5      nX-	  nUS-  S:X  a  SnO.US-  S;   a  SnO" [        SUS-
  5      n[        XA5      S:X  a  OM!  [        XU5      n[        XCU5      nSn[	        U5       H@  nU[        XgU5      -  U-  n	[        U	S	US-
  U-
  -  U5      n	X‘-  US-
  :X  d  M8  US	U-  -  nMB     [        XS-   S	-  U5      [        XgS	-  U5      -  U-  n
U
$ )
al  
Returns the square root in the case of ``p`` prime with ``p == 1 (mod 8)``

Assume that the root exists.

Parameters
==========

a : int
p : int
    prime number. should be ``p % 8 == 1``

Returns
=======

int : Generally, there are two roots, but only one is returned.
      Which one is returned is random.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _sqrt_mod_tonelli_shanks
>>> _sqrt_mod_tonelli_shanks(2, 17) in [6, 11]
True

References
==========

.. [1] Carl Pomerance, Richard Crandall, Prime Numbers: A Computational Perspective,
       2nd Edition (2005), page 101, ISBN:978-0387252827

r   é   r<   r6   )r7   r6   é   éÿÿÿÿr   r7   )r	   r   r   r$   rF   )r&   r)   ÚsrT   ÚdÚAÚDrQ   rI   Úadmr1   s              r2   Ú_sqrt_mod_tonelli_shanksrq   ª  s  € ôB 	�!�a‘%Ó€AØ	‰€Aàˆ2�v�ƒ{à‰Ø	
ˆQ‰�&‹à‰àÜ˜˜1˜q™5Ó!ˆAÜ�a‹|˜rÓ!Øñ ô
 	ˆA�!‹€AÜˆA�!‹€AØ	€AÜ�1ŽXˆØ”�A˜!“‰n˜qÑ ˆÜ�#�q˜1˜q™5 1™9‘~ qÓ)ˆØ‰7�a˜!‘eÕØ��A‘‰IŠAñ	 ô 	ˆA�A‘˜‰z˜1Óœc !¨¡T¨1›oÑ-°Ñ1€AØ€Hr4   c                óÂ   • U(       a  [        [        X5      5      $ [        [        U5      5      nUS-  nSn[        X5       H  nXS:  a  Us  $ XS:”  a  X-
  s  $ UnM     U$ )aL  
Find a root of ``x**2 = a mod p``.

Parameters
==========

a : integer
p : positive integer
all_roots : if True the list of roots is returned or None

Notes
=====

If there is no root it is returned None; else the returned root
is less or equal to ``p // 2``; in general is not the smallest one.
It is returned ``p // 2`` only if it is the only root.

Use ``all_roots`` only when it is expected that all the roots fit
in memory; otherwise use ``sqrt_mod_iter``.

Examples
========

>>> from sympy.ntheory import sqrt_mod
>>> sqrt_mod(11, 43)
21
>>> sqrt_mod(17, 32, True)
[7, 9, 23, 25]
r7   N)ÚsortedÚsqrt_mod_iterÚabsr   )r&   r)   Ú	all_rootsÚhalfpr1   Úrs         r2   Úsqrt_modry   ç  sc   € ö< Ü”m AÓ)Ó*Ð*ÜŒF�1‹I‹€AØ�‰F€EØ€AÜ˜1Ö ˆØ‹9ØŠHØ‹YØ‘5ŠLàŠAñ !ð €Hr4   c              #  ó  #   • [        U 5      [        [        U5      5      p/ n/ n[        n[        U5      R	                  5        HZ  u  pgX-  (       a  [        XU5      nO[        XU5      n[        nU(       d    gUR                  U5        UR                  Xg-  5        M\     [        U5      S:X  a  [        X#S   5       Sh  v•N   g[        U[        5      u  pšnU" U6  H  nU" [        XÄXšU[        5      5      v •  M     g N?7f)a~  
Iterate over solutions to ``x**2 = a mod p``.

Parameters
==========

a : integer
p : positive integer
domain : integer domain, ``int``, ``ZZ`` or ``Integer``

Examples
========

>>> from sympy.ntheory.residue_ntheory import sqrt_mod_iter
>>> list(sqrt_mod_iter(11, 43))
[21, 22]

See Also
========

sqrt_mod : Same functionality, but you want a sorted list or only one solution.

Nr   r   )r   ru   r   r   r#   Ú_sqrt_mod_prime_powerÚ
_sqrt_mod1r   ÚappendÚlenÚmapr   r   r   )r&   r)   ÚdomainrJ   ÚpvÚ_productr/   r0   ÚrxÚmmr*   rl   Úvxs                r2   rt   rt     sÜ   é € ô0 �!‹9”cœ& ›)“n€qØ
€AØ	€BÜ€HÜ˜A“,×$Ñ$Ö&‰ˆØ�6ä& q¨bÓ1‰Bô
 ˜A 2Ó&ˆBÜˆHÞÙØ	�‰�ŒØ
�	‰	�"‘&Öñ 'ô ˆ1ƒv�ƒ{Ü�v ™tÓ$×$Ñ$ä˜2œr“?‰ˆˆqÙ˜A“,ˆBÙœ ¨°´2Ó6Ó7Ô7ò ñ 	%ùs   ‚C DÃDÃA Dc                ó,  • X-  nX-  n US:X  a  U S-  S:w  a  gUS::  a  [        [        SUS5      5      $ Sn[        SU5       H#  nUS-  U -
  U-	  S-  (       d  M  USUS-
  -  -  nM%     SUS-
  -  n[        XCU-
  XF-   U-  XF-   * U-  /5      $ [        X5      S:w  a  gUS-  S:X  a  [	        XS-   S-  U5      nOSUS-  S:X  a?  [	        XS-   S-  U5      n[	        USU5      X-  :w  a  U[	        SUS-
  S-  U5      -  U-  nO[        X5      nUS:”  at  Un[        UR                  5       S-
  5       H'  n	US-  n[        SU-  U5      n
XwS-  U -
  U
-  -
  U-  nM)     X"S-
  -  (       a  [        SU-  U5      n
XwS-  U -
  U
-  -
  U-  n[        XsU-
  /5      $ )a  
Find the solutions to ``x**2 = a mod p**k`` when ``a % p != 0``.
If no solution exists, return ``None``.
Solutions are returned in an ascending list.

Parameters
==========

a : integer
p : prime number
k : positive integer

Examples
========

>>> from sympy.ntheory.residue_ntheory import _sqrt_mod_prime_power
>>> _sqrt_mod_prime_power(11, 43, 1)
[21, 22]

References
==========

.. [1] P. Hackman "Elementary Number Theory" (2009), page 160
.. [2] http://www.numbertheory.org/php/squareroot.html
.. [3] [Gathen99]_
r7   r8   r   Nr6   rZ   r<   )ÚlistrF   rs   r   r$   rq   Ú
bit_lengthr   )r&   r)   rK   Úpkrx   ÚnxÚhÚresr/   Ú_Úfrinvs              r2   r{   r{   F  sâ  € ð6 
‰€BØ	‰€AàˆAƒvàˆq‰5�A‹:Øà�‹6Üœ˜a  Q›Ó(Ð(Øˆô
 ˜˜1–+ˆBØ�A‘˜‘˜bÑ  A×%Ñ%Ø�Q˜2 ™6‘]Ñ"’ñ ð
 �!�a‘%‰LˆÜ�q˜q™& 1¡5¨B¡,°!±%°¸2±Ð>Ó?Ð?ô ˆaƒ|�qÓØØˆ1�u�ƒzÜ�!˜!‘e ‘\ 1Ó%‰Ø	
ˆQ‰�!‹Ü�!˜!‘e ‘\ 1Ó%ˆÜˆs�A�q‹>˜Q™UÓ"Øœ˜A  A¡¨!™|¨QÓ/Ñ/°!Ñ3ˆCøä& qÓ,ˆØˆ1ƒuð ˆÜ�q—|‘|“~¨Ñ)Ö*ˆAØ�Q‘ˆBÜ˜1˜S™5 "Ó%ˆEØ˜q™& 1™* eÑ+Ñ+¨rÑ1ŠCñ +ð �A‘�;Ü˜1˜S™5 "Ó%ˆEØ˜q™& 1™* eÑ+Ñ+¨rÑ1ˆCÜ�3˜S™�/Ó"Ð"r4   c                óÖ   ^^^^• TT-  mU T-  n U S:X  a  [        STTTS-   S-  -  5      $ [        U T5      u  pUS-  S:X  a  g[        U TTU-
  5      nUc  gUS-  mUUUU4S jU 5       $ )a  
Find solution to ``x**2 == a mod p**n`` when ``a % p == 0``.
If no solution exists, return ``None``.

Parameters
==========

a : integer
p : prime number, p must divide a
n : positive integer

References
==========

.. [1] http://www.numbertheory.org/php/squareroot.html
r   r   r7   Nc              3  óh   >#   • U  H'  n[        UTT-  -  TTTT-
  -  5        H  o"v •  M     M)     g 7frO   ©rF   )r@   rƒ   r1   rQ   r'   r)   Úpns      €€€€r2   rC   Ú_sqrt_mod1.<locals>.<genexpr>¯  s3   øé € ÐDš�"¤U¨2¨a°©d©7°B¸¸AÀ¹E¹
×%C ŒAÑ%C‰Ašùs   ƒ/2)rF   r
   r{   )r&   r)   r'   rx   rŒ   rQ   r’   s    ``  @@r2   r|   r|   ‘  sƒ   û€ ð" 
ˆA‰€BØ	ˆB‰€AØˆAƒvä�Q˜˜A  Q¡¨1¡Ñ-Ó.Ð.ä�!�Q‹<�D€AØˆ1�u�ƒzØÜ
  1 a¨!¡eÓ
,€CØ
�{ØØ	ˆQ‰€AßD™ÓDÐDr4   c                óX  • [        U 5      [        U5      pUS:  a  [        S5      eX-  n U S:  d  US:  a  g[        U5      nU(       aF  U SU-  -  nU(       a"  [        U5      nUS-  (       d  X4-	  S-  (       a  gX-  nX-  n U S:  d  US:  a  g[        X5      nUS:X  d  [	        U5      (       a  US:H  $ [        U5      R                  5        HX  u  pgX-  (       a  [        X5      S:w  a    gM!  XU-  -  nUS	:X  a  M0  [        X65      u  p4US-  (       d  [        X65      S:w  d  MX    g   g)
a}  
Returns True if ``a`` (mod ``p``) is in the set of squares mod ``p``,
i.e a % p in set([i**2 % p for i in range(p)]).

Parameters
==========

a : integer
p : positive integer

Returns
=======

bool : If True, ``x**2 == a (mod p)`` has solution.

Raises
======

ValueError
    If ``a``, ``p`` is not integer.
    If ``p`` is not positive.

Examples
========

>>> from sympy.ntheory import is_quad_residue
>>> is_quad_residue(21, 100)
True

Indeed, ``pow(39, 2, 100)`` would be 21.

>>> is_quad_residue(21, 120)
False

That is, for any integer ``x``, ``pow(x, 2, 120)`` is not 21.

If ``p`` is an odd
prime, an iterative method is used to make the determination:

>>> from sympy.ntheory import is_quad_residue
>>> sorted(set([i**2 % 7 for i in range(7)]))
[0, 1, 2, 4]
>>> [j for j in range(7) if is_quad_residue(j, 7)]
[0, 1, 2, 4]

See Also
========

legendre_symbol, jacobi_symbol, sqrt_mod
r   zp must be > 0r7   r6   Trj   Frk   r   )r   r"   r	   r   r   r   r#   r
   )r&   r)   rT   Úa_rx   Újr/   r0   s           r2   Úis_quad_residuer—   ²  s%  € ôf �!‹9”f˜Q“i€qØˆ1ƒuÜ˜Ó)Ð)Ø�F€AØˆ1ƒu��A“Øô 	�!‹€AÞð �!�q‘&‰\ˆÞÜ˜"“ˆAØ�1�u˜™ AŸØØ	‰ˆØ	‰ˆØˆq‹5�A˜“EØäˆq‹€AØˆBƒw”'˜!—*‘*Ø�A‰vˆä˜A“,×$Ñ$Ö&‰ˆØ�6Ü�a‹} Ó!Ùñ "ð ˜‘V‘ˆBØ�Q‹wÙÜ˜2“N‰EˆBØ�1�uœ˜r›¨!Õ+Ùñ 'ð r4   c                óV  ^ ^• T U-  m [        T 5      [        T5      [        U5      sm mnUS::  a  [        S5      eTS:  a  [        S5      eTS:X  a  US:X  a  gT S:H  $ T S:X  a  gTS:X  a  gTS:X  a  [        T U5      $ [        U U4S j[	        U5      R                  5        5       5      $ )	zŒ
Returns True if ``x**n == a (mod m)`` has solutions.

References
==========

.. [1] P. Hackman "Elementary Number Theory" (2009), page 76

r   zm must be > 0zn must be >= 0r   FTr7   c              3  óB   >#   • U  H  u  p[        TTX5      v •  M     g 7frO   )Ú#_is_nthpow_residue_bign_prime_power)r@   r)   r*   r&   r'   s      €€r2   rC   Ú$is_nthpow_residue.<locals>.<genexpr>'  s&   øé € ð 1Ú/‘4�1ô 3°1°a¸×>Ð>Ú/ùs   ƒ)r   r"   r—   rG   r   r#   )r&   r'   rQ   s   `` r2   Úis_nthpow_residuerœ     s·   ù€ ð 	
ˆA‰€AÜ�Q‹iœ ›¤F¨1£I€G€A€qˆ!ØˆAƒvÜ˜Ó)Ð)Øˆ1ƒuÜÐ)Ó*Ð*ØˆAƒvØ�‹6ØØ�A‰vˆØˆAƒvØØˆAƒvØØˆAƒvÜ˜q !Ó$Ð$Üõ 1Ü$ Q›<×-Ñ-Ô/ó1ó 1ð 1r4   c                ó`  • X-  S:X  a;  U [        X#5      -  n U (       d  g[        X5      u  pXA-  (       a  gX4-  nX-  S:X  a  M;  US:w  a1  X#S-
  -  US-
  -  n[        X[        XQ5      -  [        X#5      5      S:H  $ US-  (       a  g[        [	        U5      S-   U5      nU [        SU5      -  S:H  $ )z¨
Returns True if `x^n = a \pmod{p^k}` has solutions for `n > 2`.

Parameters
==========

a : positive integer
n : integer, n > 2
p : prime number
k : positive integer

r   TFr7   r   )r$   r
   r   Úminr	   )r&   r'   r)   rK   ÚmuÚfÚcs          r2   rš   rš   +  s´   € ð ‰%�1‹*Ø	ŒS�‹Y‰ˆÞØÜ�q“‰ˆØ�6ØØ	‰ˆð ‰%�1�*ð 	ˆAƒvØ�A‘‰J˜˜A™ÑˆÜ�1œ3˜q›9‘n¤c¨!£iÓ0°AÑ5Ð5Øˆ1‡uØÜŒI�a‹L˜1Ñ˜aÓ €AØŒs�1�a‹y‰=˜AÑÐr4   c           
     ór  • [        [        U5      5      nU n[        U5      R                  5        H�  u  pgUS-
  Xg-  -  nX†-  S:X  a  X†-  nX†-  S:X  a  M  U[	        U* U5      -  n	SU	-   U-  n
[        U[        XXU5      [        XHU-  U5      5      n[        U5       H-  n[        XZU5      [        XI* U-  US-
  -  U5      -  U-  nX¶-  nM/     MŸ     U/n[        XBS-
  U-  U5      nUn[        US-
  5       H  nXþ-  U-  nUR                  U5        M     U(       a  UR                  5         U$ [        U5      $ )aá  
Root of ``x**q = s mod p``, ``p`` prime and ``q`` divides ``p - 1``.
Assume that the root exists.

Parameters
==========

s : integer
q : integer, n > 2. ``q`` divides ``p - 1``.
p : prime number
all_roots : if False returns the smallest root, else the list of roots

Returns
=======

list[int] | int :
    Root of ``x**q = s mod p``. If ``all_roots == True``,
    returned ascending list. otherwise, returned an int.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _nthroot_mod1
>>> _nthroot_mod1(5, 3, 13, False)
7
>>> _nthroot_mod1(13, 4, 17, True)
[3, 5, 12, 14]

References
==========

.. [1] A. M. Johnston, A Generalized qth Root Algorithm,
       ACM-SIAM Symposium on Discrete Algorithms (1999), pp. 929-930

r   r   )r[   rL   r   r#   r   Údiscrete_logr$   rF   r}   Úsortrž   )rl   r_   r)   rv   rB   rx   Úqxr0   r    Úzr1   rT   r�   rŒ   r‹   Úhxs                   r2   Ú_nthroot_mod1r¨   I  s@  € ôH 	Ô'¨Ó*Ó+€AØ	€AÜ˜A“,×$Ñ$Ö&‰ˆØ�‰U�r‘vÑˆØ‰f˜‹kØ‰HˆAð ‰f˜�kàŒf�a�R˜‹nÑˆØ�‰U�r‰MˆÜ˜œC  a›L¬#¨a°2±°q«/Ó:ˆÜ�r–ˆAä�A˜!“œS  B q¡D¨A°©E¡N°AÓ6Ñ6¸Ñ:ˆAØ‰HŠAó ñ 'ð ˆ#€CÜˆA�A‘˜!‰|˜QÓ€Aà	
€BÜ�1�q‘5Ž\ˆØ‰d�a‰ZˆØ�
‰
�2Žñ ö Ø�‰Œ
Øˆ
Üˆs‹8€Or4   c                óv  • [        XX#5      (       d  / $ X-  nUS:X  a  S/nO‡US-
  U-  S:X  a  [        XAUSS9nOoUnUS-
  nSnXg:  a  X‡XF4u  pFp‡U(       a-  [        Xg5      u  pš[        X‰* U5      U-  U-  nXzpvX‹p„U(       a  M-  US:X  a  U/nOUS:X  a  [	        XBSS9nO[        XFUSS9nUS:X  a  U$ XU-  -  n [        5       nU Hè  n[        XÑS-
  U5      U-  U-  nUnUS:w  aS  [        Xâ5      n[        US-
  5       H#  nXò-  n[        XÑU5      U -
  nUU-  nUU-
  U-  nM%     UR                  U5        Ms  U1n[        US-
  5       H[  nXò-  n[        5       nU HB  n[        UX5      X-  :w  a  M  UU;  d  M  UR                  U5        UXò-  -   U-  nUU;  a  M#  MD     UnM]     UU-  nMê     [        U5      $ )zûRoot of ``x**n = a mod p**k``.

Parameters
==========

a : integer
n : integer, n > 2
p : prime number
k : positive integer

Returns
=======

list[int] :
    Ascending list of roots of ``x**n = a mod p**k``.
    If no solution exists, return ``[]``.

r   r   T)rv   r7   )
rš   r¨   Údivmodr$   ry   rf   r   rF   rg   rs   )r&   r'   r)   rK   Úa_mod_pÚ
base_rootsÚpaÚpbÚbr_   Úpcr¡   Ú	tot_rootsÚrootÚdiffÚnew_baseÚm_invr�   ÚtmpÚroots_in_baseÚ	new_rootsÚk_s                         r2   Ú_nthroot_mod_prime_powerrº   ‡  s  € ô& /¨q°Q×:Ñ:Øˆ	Ø‰e€GØ�!ƒ|Ø�S‰
Ø
ˆa‰%�1‰˜Ó	Ü" 7¨q¸DÑA‰
ð ˆØ�‰UˆØˆØ‹7Ø!"¨Ð!3ÑˆG˜ö Ü˜2“N‰EˆAÜ�A�r˜1“ Ñ'¨!Ñ+ˆAØ�Ø�Q÷	 ˆbð
 �‹7Ø!˜‰JØ�1‹WÜ! '¸Ñ=‰Jä& w°AÀÑFˆJØˆAƒvØÐØˆA‰�I€AÜ“€IÛˆÜ�4˜Q™ Ó" 1Ñ$ qÑ(ˆØˆØ�1‹9Ü˜4“OˆEÜ˜1˜q™5–\�Ø‘�Ü˜$ 8Ó,¨qÑ0�Ø�u‘�Ø˜s™
 hÑ.’ñ	 "ð
 �M‰M˜$Öà!˜FˆMÜ˜1˜q™5–\�Ø‘�Ü›E�	Û'�BÜ˜2˜qÓ+¨q©|Ó;Ù Ø IÕ-Ø!Ÿ™ bÔ)Ø  H¡MÑ2°hÑ>˜ð  I×-ñ (ð !*’ñ "ð " MÑ1ŠIñ/ ô0 �)ÓÐr4   c                óœ  • X-  n [        U 5      [        U5      [        U5      p!n US:  a  [        S5      eUS:  a  [        S5      eUS:X  a  U(       a  U /$ U $ US:X  a  [        XU5      $ / n/ n[        U5      R	                  5        HT  u  pg[        XXg5      nU(       d  U(       a  / s  $ Ss  $ UR                  Xg-  5        UR                  [        U5      5        MV     [        U[        5      u  pšn[        [        [        [        U6  Vs1 s H  n[        XÅXšU[        5      iM     sn5      5      nU(       a  U$ U(       a  US   $ gs  snf )aõ  
Find the solutions to ``x**n = a mod p``.

Parameters
==========

a : integer
n : positive integer
p : positive integer
all_roots : if False returns the smallest root, else the list of roots

Returns
=======

    list[int] | int | None :
        solutions to ``x**n = a mod p``.
        The table of the output type is:

        ========== ========== ==========
        all_roots  has roots  Returns
        ========== ========== ==========
        True       Yes        list[int]
        True       No         []
        False      Yes        int
        False      No         None
        ========== ========== ==========

Raises
======

    ValueError
        If ``a``, ``n`` or ``p`` is not integer.
        If ``n`` or ``p`` is not positive.

Examples
========

>>> from sympy.ntheory.residue_ntheory import nthroot_mod
>>> nthroot_mod(11, 4, 19)
8
>>> nthroot_mod(11, 4, 19, True)
[8, 11]
>>> nthroot_mod(68, 3, 109)
23

References
==========

.. [1] P. Hackman "Elementary Number Theory" (2009), page 76

r   ún should be positivezp should be positiver7   Nr   )r   r"   ry   r   r#   rº   r}   rs   r   r   r   r%   r   r   )r&   r'   r)   rv   ÚbaseÚprime_powerr_   r*   r±   ÚPÚEÚSr¡   Úrets                 r2   Únthroot_modrÃ   Õ  s@  € ðh 	
‰€AÜ�Q‹iœ ›¤F¨1£Iˆ!€Aàˆ1ƒuÜÐ/Ó0Ð0Øˆ1ƒuÜÐ/Ó0Ð0ØˆAƒvÞ�ˆsÐ& QÐ&ØˆAƒvÜ˜˜iÓ(Ð(Ø€DØ€KÜ˜!“×"Ñ"Ö$‰ˆÜ,¨Q°1Ó8ˆ	ÞÞ"�2Ò,¨Ò,Ø×Ñ˜1™4Ô Ø�‰”F˜9Ó%Ö&ñ %ô �k¤2Ó&�G€Aˆ!Ü
””SÜ$+¨T¡Nó4Ú$2˜qô # 1°1¸¼BÖ?Ù$2ñ4ó 5ó 6€CæØˆ
Þ
Ø�1‰vˆð ùò	4s   ÄE	c                ó�   • [        U 5      n [        U S-  S-   5       Vs1 s H  n[        USU 5      iM     nn[        U5      $ s  snf )z¥
Returns the list of quadratic residues.

Examples
========

>>> from sympy.ntheory.residue_ntheory import quadratic_residues
>>> quadratic_residues(7)
[0, 1, 2, 4]
r7   r   )r   rF   r$   rs   )r)   rI   rx   s      r2   Úquadratic_residuesrÅ   %  sG   € ô 	ˆq‹	€AÜ$ Q¨!¡V¨a¡ZÔ0Ó1Ò0˜!ŒˆQ��1ŽÑ0€AÐ1Ü�!‹9Ðùò 	2s   ŸAz~The `sympy.ntheory.residue_ntheory.legendre_symbol` has been moved to `sympy.functions.combinatorial.numbers.legendre_symbol`.z1.13z%deprecated-ntheory-symbolic-functions)Údeprecated_since_versionÚactive_deprecations_targetc                ó   • SSK Jn  U" X5      $ )aÂ  
Returns the Legendre symbol `(a / p)`.

.. deprecated:: 1.13

    The ``legendre_symbol`` function is deprecated. Use :class:`sympy.functions.combinatorial.numbers.legendre_symbol`
    instead. See its documentation for more information. See
    :ref:`deprecated-ntheory-symbolic-functions` for details.

For an integer ``a`` and an odd prime ``p``, the Legendre symbol is
defined as

.. math ::
    \genfrac(){}{}{a}{p} = \begin{cases}
         0 & \text{if } p \text{ divides } a\\
         1 & \text{if } a \text{ is a quadratic residue modulo } p\\
        -1 & \text{if } a \text{ is a quadratic nonresidue modulo } p
    \end{cases}

Parameters
==========

a : integer
p : odd prime

Examples
========

>>> from sympy.functions.combinatorial.numbers import legendre_symbol
>>> [legendre_symbol(i, 7) for i in range(7)]
[0, 1, 1, -1, 1, -1, -1]
>>> sorted(set([i**2 % 7 for i in range(7)]))
[0, 1, 2, 4]

See Also
========

is_quad_residue, jacobi_symbol

r   )Úlegendre_symbol)Ú%sympy.functions.combinatorial.numbersrÉ   )r&   r)   Ú_legendre_symbols      r2   rÉ   rÉ   5  s   € õZ ZÙ˜AÓ!Ð!r4   zzThe `sympy.ntheory.residue_ntheory.jacobi_symbol` has been moved to `sympy.functions.combinatorial.numbers.jacobi_symbol`.c                ó   • SSK Jn  U" X5      $ )a!  
Returns the Jacobi symbol `(m / n)`.

.. deprecated:: 1.13

    The ``jacobi_symbol`` function is deprecated. Use :class:`sympy.functions.combinatorial.numbers.jacobi_symbol`
    instead. See its documentation for more information. See
    :ref:`deprecated-ntheory-symbolic-functions` for details.

For any integer ``m`` and any positive odd integer ``n`` the Jacobi symbol
is defined as the product of the Legendre symbols corresponding to the
prime factors of ``n``:

.. math ::
    \genfrac(){}{}{m}{n} =
        \genfrac(){}{}{m}{p^{1}}^{\alpha_1}
        \genfrac(){}{}{m}{p^{2}}^{\alpha_2}
        ...
        \genfrac(){}{}{m}{p^{k}}^{\alpha_k}
        \text{ where } n =
            p_1^{\alpha_1}
            p_2^{\alpha_2}
            ...
            p_k^{\alpha_k}

Like the Legendre symbol, if the Jacobi symbol `\genfrac(){}{}{m}{n} = -1`
then ``m`` is a quadratic nonresidue modulo ``n``.

But, unlike the Legendre symbol, if the Jacobi symbol
`\genfrac(){}{}{m}{n} = 1` then ``m`` may or may not be a quadratic residue
modulo ``n``.

Parameters
==========

m : integer
n : odd positive integer

Examples
========

>>> from sympy.functions.combinatorial.numbers import jacobi_symbol, legendre_symbol
>>> from sympy import S
>>> jacobi_symbol(45, 77)
-1
>>> jacobi_symbol(60, 121)
1

The relationship between the ``jacobi_symbol`` and ``legendre_symbol`` can
be demonstrated as follows:

>>> L = legendre_symbol
>>> S(45).factors()
{3: 2, 5: 1}
>>> jacobi_symbol(7, 45) == L(7, 3)**2 * L(7, 5)**1
True

See Also
========

is_quad_residue, legendre_symbol
r   )Újacobi_symbol)rÊ   rÍ   )rQ   r'   Ú_jacobi_symbols      r2   rÍ   rÍ   f  s   € õF VÙ˜!ÓÐr4   zlThe `sympy.ntheory.residue_ntheory.mobius` has been moved to `sympy.functions.combinatorial.numbers.mobius`.c                ó   • SSK Jn  U" U 5      $ )a>  
Mobius function maps natural number to {-1, 0, 1}

.. deprecated:: 1.13

    The ``mobius`` function is deprecated. Use :class:`sympy.functions.combinatorial.numbers.mobius`
    instead. See its documentation for more information. See
    :ref:`deprecated-ntheory-symbolic-functions` for details.

It is defined as follows:
    1) `1` if `n = 1`.
    2) `0` if `n` has a squared prime factor.
    3) `(-1)^k` if `n` is a square-free positive integer with `k`
       number of prime factors.

It is an important multiplicative function in number theory
and combinatorics.  It has applications in mathematical series,
algebraic number theory and also physics (Fermion operator has very
concrete realization with Mobius Function model).

Parameters
==========

n : positive integer

Examples
========

>>> from sympy.functions.combinatorial.numbers import mobius
>>> mobius(13*7)
1
>>> mobius(1)
1
>>> mobius(13*7*5)
-1
>>> mobius(13**2)
0

References
==========

.. [1] https://en.wikipedia.org/wiki/M%C3%B6bius_function
.. [2] Thomas Koshy "Elementary Number Theory with Applications"

r   )Úmobius)rÊ   rÐ   )r'   Ú_mobiuss     r2   rÐ   rÐ   ­  s   € õd HÙ�1‹:Ðr4   Nc                óz   • X-  nX -  nUc  U nSn[        U5       H  nXA:X  a  Us  $ XB-  U -  nM     [        S5      e)ag  
Trial multiplication algorithm for computing the discrete logarithm of
``a`` to the base ``b`` modulo ``n``.

The algorithm finds the discrete logarithm using exhaustive search. This
naive method is used as fallback algorithm of ``discrete_log`` when the
group order is very small. The value ``n`` must be greater than 1.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _discrete_log_trial_mul
>>> _discrete_log_trial_mul(41, 15, 7)
3

See Also
========

discrete_log

References
==========

.. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).
r   úLog does not exist)rF   r"   )r'   r&   r¯   r.   r1   rI   s         r2   Ú_discrete_log_trial_mulrÔ   ã  sR   € ð6 �F€AØ�F€AØ�}ØˆØ	€AÜ�5Ž\ˆØ‹6ØŠHØ‰E�A‰IŠñ ô Ð)Ó
*Ð*r4   c                ó  • X-  nX -  nUc  [        X 5      n[        U5      S-   n0 nSn[        U5       H  nXuU'   Xb-  U -  nM     [        X$* U 5      nUn[        U5       H  nXe;   a  Xt-  XV   -   s  $ Xh-  U -  nM     [	        S5      e)a!  
Baby-step giant-step algorithm for computing the discrete logarithm of
``a`` to the base ``b`` modulo ``n``.

The algorithm is a time-memory trade-off of the method of exhaustive
search. It uses `O(sqrt(m))` memory, where `m` is the group order.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _discrete_log_shanks_steps
>>> _discrete_log_shanks_steps(41, 15, 7)
3

See Also
========

discrete_log

References
==========

.. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).
r   rÓ   )r3   r   rF   r$   r"   )	r'   r&   r¯   r.   rQ   ÚTr1   rI   r¦   s	            r2   Ú_discrete_log_shanks_stepsr×   
  s¥   € ð4 �F€AØ�F€AØ�}Ü˜“ˆÜˆU‹�a‰€AØ
€AØ	€AÜ�1ŽXˆØˆ!‰Ø‰E�A‰IŠñ ô 	ˆAˆr�1‹€AØ	€AÜ�1ŽXˆØ‹6Ø‘5˜1™4‘<ÒØ‰E�A‰IŠñ ô Ð)Ó
*Ð*r4   c                óî  • X-  nX -  nUc  [        X 5      n[        U5      n[        U5       GHª  nU" SUS-
  5      nU" SUS-
  5      n	[        X(U 5      [        XU 5      -  U -  n
U
S-  nUS:X  a  X-  U -  nUnU	S-   U-  nO-US:X  a  Xª-  U -  nXˆ-   U-  nX™-   U-  nOX*-  U -  nUS-   U-  nU	n[        U5       GH  nU
S-  nUS:X  a  X-  U -  n
U	S-   U-  n	O+US:X  a  Xª-  U -  n
Xˆ-   U-  nX™-   U-  n	OX*-  U -  n
US-   U-  nUS-  nUS:X  a  X-  U -  nUS-   U-  nO+US:X  a  XÌ-  U -  nXÝ-   U-  nXî-   U-  nOX,-  U -  nUS-   U-  nUS-  nUS:X  a  X-  U -  nUS-   U-  nO+US:X  a  XÌ-  U -  nXÝ-   U-  nXî-   U-  nOX,-  U -  nUS-   U-  nX¬:X  d  MÝ  Xž-
  U-  n [	        UU5      XØ-
  -  U-  n[        UUU 5      U-
  U -  S:X  a  Us  s  $    GM¨     GM­     [        S5      e! [
         a     N"f = f)a#  
Pollard's Rho algorithm for computing the discrete logarithm of ``a`` to
the base ``b`` modulo ``n``.

It is a randomized algorithm with the same expected running time as
``_discrete_log_shanks_steps``, but requires a negligible amount of memory.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _discrete_log_pollard_rho
>>> _discrete_log_pollard_rho(227, 3**7, 3)
7

See Also
========

discrete_log

References
==========

.. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).
r   r6   r   z&Pollard's Rho failed to find logarithm)r3   r   rF   r$   r   ÚZeroDivisionErrorr"   )r'   r&   r¯   r.   ÚretriesÚrseedr   rI   ÚaaÚbaÚxar¡   ÚxbÚabÚbbr–   rx   r*   s                     r2   Ú_discrete_log_pollard_rhorâ   7  sµ  € ð4 �F€AØ�F€Aà�}Ü˜“ˆÜ�u‹o€Gä�7�^ˆÙ�Q˜ ™	Ó"ˆÙ�Q˜ ™	Ó"ˆÜ�˜‹]œS ¨›]Ñ*¨QÑ.ˆà�‰FˆØ�‹6Ø‘˜!‘ˆBØˆBØ�q‘&˜EÑ!‰BØ�!‹VØ‘˜1‘ˆBØ‘'˜UÑ"ˆBØ‘'˜UÑ"‰Bà‘˜!‘ˆBØ�q‘&˜EÑ!ˆBØˆBä�u—ˆAØ�Q‘ˆAØ�A‹vØ‘V˜a‘Z�Ø˜1‘f Ñ%‘Ø�a“Ø‘W˜q‘[�Ø‘g Ñ&�Ø‘g Ñ&‘à‘V˜a‘Z�Ø˜1‘f Ñ%�à�Q‘ˆAØ�A‹vØ‘V˜a‘Z�Ø˜1‘f Ñ%‘Ø�a“Ø‘W˜q‘[�Ø‘g Ñ&�Ø‘g Ñ&‘à‘V˜a‘Z�Ø˜1‘f Ñ%�à�Q‘ˆAØ�A‹vØ‘V˜a‘Z�Ø˜1‘f Ñ%‘Ø�a“Ø‘W˜q‘[�Ø‘g Ñ&�Ø‘g Ñ&‘à‘V˜a‘Z�Ø˜1‘f Ñ%�à�xØ‘W Ñ%�ðÜ˜q %Ó(¨B©GÑ4°uÑ<�AÜ˜A˜q !› qÑ(¨AÑ-°Ó2Ø œð 3ó ô[ ñ' ôB Ð=Ó
>Ð>øô )ó Ùðús   Æ,G'Ç'
G4Ç3G4c                ó¢   • S/[        U5      -  n[        U5       H)  u  p4X-  S:X  d  M  X#==   S-  ss'   X-  n X-  S:X  a  M  M+     U S:w  a  gU$ )z�Try to factor n with respect to a given factorbase.
Upon success a list of exponents with respect to the factorbase is returned.
Otherwise None.r   r   N)r~   Ú	enumerate)r'   Ú
factorbaser-   rI   r)   s        r2   Ú_discrete_log_is_smoothræ   œ  s\   € ð ˆc”#�j“/Ñ!€GÜ˜*Ö%‰ˆØ‰e�q�jØ‹J˜!‰O‹JØ‘ˆAð ‰e�q�jñ &ð 	ˆAƒvØØ€Nr4   c                ó>  • [        U5      nSSKJnJnJn  X-  nX -  n[        U" SU" U" U 5      U" U" U 5      5      -  5      -  SSU" U" U 5      5      -  -   -  5      5      n	SU	-  U	-  n
[        [        U	5      5      n[        U5      nUS-
  nUn[        U5       HG  nUS:X  a	  X?-
  U-  s  $ [        Xë5      nU(       a  U Vs/ s H  nUU-  PM
     snU/-   n  OXâ-  U -  nMI     [        S5      eS/U-  nSnSnUSU-  :  GaŠ  UU
:  Gaƒ  U" SU5      n[        [        X/U 5      U5      nUc  US-  nM:  US-  nSnUU/-  nUn[        U5       H^  nUU   U-  nUS:”  a3  UU   b-  [        US-   5       H  nUU   UUU   U   -  -
  U-  UU'   M     OUUU'   UU   S:”  d  MT  UU:X  d  M\  UnM`     UU:X  d  UU   b  MÄ  [        UU   S	U5      n[        UUS-   5       H  nUUU   -  U-  UU'   M     UUU'   [        U5       HN  nUU   S:”  a7  UU   b1  UU   n[        US-   5       H  nUU   UUU   U   -  -
  U-  UU'   M     UU   S:”  d  MN    O*   UUU   -
  U-  n[        X/U 5      U:X  a  U$ [        S5      eUSU-  :  a	  UU
:  a  GMƒ  [        S5      es  snf )
aP  
Index Calculus algorithm for computing the discrete logarithm of ``a`` to
the base ``b`` modulo ``n``.

The group order must be given and prime. It is not suitable for small orders
and the algorithm might fail to find a solution in such situations.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _discrete_log_index_calculus
>>> _discrete_log_index_calculus(24570203447, 23859756228, 2, 12285101723)
4519867240

See Also
========

discrete_log

References
==========

.. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).
r   )r   ÚexpÚlogg      à?r   r<   zIndex Calculus failedNr6   rk   )r   Úmathr   rè   ré   r%   r‡   r   r~   rF   ræ   r"   r$   )r'   r&   r¯   r.   rÛ   r   r   rè   ré   ÚBÚmaxrå   ÚlfÚordermoÚabxr1   Ú	relationarx   Ú	relationsrK   ÚkkÚrelationÚindexrI   Úrir–   ÚrinvÚrbis                               r2   Ú_discrete_log_index_calculusrø   ª  s"  € ô4 �u‹o€Gß#Ñ#Ø�F€AØ�F€Aô
 	‰C�‘d™C ›F¡S©¨Q«£[Ñ0Ó2Ñ2°Q¸¹3¹sÀ1»v»;¹Ñ5FÑHÓIÓJ€AØ
ˆa‰%�!‰)€CÜ”j “mÓ$€JÜ	ˆZ‹€BØ�A‰g€GØ
€CÜ�5Ž\ˆØ�!‹8Ø‘I Ñ&Ò&Ü+¨CÓ<ˆ	ÞÙ,5Ó6ªI q˜˜Uœ©IÑ6¸!¸Ñ<ˆIÙØ‰g˜‰kŠñ ô Ð0Ó1Ð1à�˜‘€IØ	€AØ	
€BØ
ˆa�"‰fŒ*˜˜cœÙ�A�gÓˆÜ*¬3¨q°1«:°zÓBˆØÑØ�!‰GˆBÙØ	ˆQ‰ˆØˆØ�a�EÑˆØˆÜ�r–ˆAØ˜!‘˜uÑ$ˆBØ�1‹u˜ 1™Ñ1Ü˜r !™tž�AØ#+¨A¡;°°I¸a±LÀ±OÑ1CÑ#CÀuÑ"L�H˜Q“Kò %ð !�˜‘Ø˜‰{˜Q� 5¨B¥;Ø’ñ ð �B‹;˜) EÑ*Ñ6Ùä�8˜E‘? 2 eÓ,ˆÜ�u˜R ™TÖ"ˆAØ ¨!¡Ñ,¨uÑ4ˆH�Q‹Kñ #à#ˆ	�%ÑÜ�r–ˆAØ˜‰|˜aÓ I¨a¡LÑ$<Ø ‘l�Ü˜r !™tž�AØ$-¨a¡L°3°yÀ±|ÀA±Ñ3FÑ$FÈ%Ñ#O�I˜a“Lñ %à˜‰|˜aÕÙñ ð ˜	 "™Ñ%¨Ñ.ˆAÜ�1�q‹z˜Q‹Ø�ÜÐ4Ó5Ð5ðM ˆa�"‰f‹*˜˜cžôN Ð,Ó
-Ð-ùòc 7s   ÃJc           
     óø  • SSK Jn  X-  nX -  nUc  [        X 5      nUc  [        U5      nS/[	        U5      -  n[        UR                  5       5       Hm  u  nu  p‰[        U	5       HV  n
[        U[        X&U   * U 5      -  X8U
S-   -  -  U 5      n[        X#U-  U 5      n[        XXÈS5      nXg==   XØU
-  -  -  ss'   MX     Mo     U" UR                  5        VV	s/ s H	  u  p‰X‰-  PM     sn	nU5      u  pïU$ s  sn	nf )aY  
Pohlig-Hellman algorithm for computing the discrete logarithm of ``a`` to
the base ``b`` modulo ``n``.

In order to compute the discrete logarithm, the algorithm takes advantage
of the factorization of the group order. It is more efficient when the
group order factors into many small primes.

Examples
========

>>> from sympy.ntheory.residue_ntheory import _discrete_log_pohlig_hellman
>>> _discrete_log_pohlig_hellman(251, 210, 71)
197

See Also
========

discrete_log

References
==========

.. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).
r   r   r   T)
Úmodularr   r3   r   r~   rä   r#   rF   r$   r£   )r'   r&   r¯   r.   Úorder_factorsr   ÚlrI   Úpirõ   r–   ÚajÚbjÚcjrm   r�   s                   r2   Ú_discrete_log_pohlig_hellmanr    s  € õ6 Ø�F€AØ�F€Aà�}Ü˜“ˆØÑÜ! %Ó(ˆØ	
ˆŒc�-Ó Ñ €Aä  ×!4Ñ!4Ó!6Ö7‰ˆ‰8ˆBÜ�r–ˆAÜ�Qœ˜Q 1¡  qÓ)Ñ)¨5¸¸Q¹±KÑ+?ÀÓCˆBÜ�Q ™ QÓ'ˆBÜ˜a R¨TÓ2ˆBØ‹D�B˜Q™‘JÑ�Dó	 ñ 8ñ  ]×%8Ñ%8Ô%:Ô;Ò%:™6˜2�”Ñ%:Ò;¸QÓ?�D€AØ€Hùó <s   ÃC6
c           	     óš  • SSK JnJn  [        U 5      [        U5      [        U5      p!n U S:  a  [	        S5      eU S:X  a  gUcþ  0 n[        U 5      R                  5        Hc  u  p‰U	S:”  a  X‡;   a  Xx==   U	S-
  -  ss'   OU	S-
  Xx'   [        US-
  5      R                  5        H  u  p«X§;   a  Xz==   U-  ss'   M  X·U
'   M     Me     SnUR                  5        H  u  p‰X8U	-  -  nM     0 nUR                  5        HD  u  pÞSn[        U5       H!  n[        X#U-  U 5      S:X  a  X=-  nUS-  nM!    O   Xþ:  d  M>  Xï-
  XÍ'   MF     Uc  [        U5      nUS:  a  [        XX#5      $ U(       aW  SU" U" U 5      U" U" U 5      5      -  5      -  U" U5      S-
  :  a  [        XX#5      $ US:  a  [        XX#5      $ [        XX#5      $ [        XX#W5      $ )	aØ  
Compute the discrete logarithm of ``a`` to the base ``b`` modulo ``n``.

This is a recursive function to reduce the discrete logarithm problem in
cyclic groups of composite order to the problem in cyclic groups of prime
order.

It employs different algorithms depending on the problem (subgroup order
size, prime order or not):

    * Trial multiplication
    * Baby-step giant-step
    * Pollard's Rho
    * Index Calculus
    * Pohlig-Hellman

Examples
========

>>> from sympy.ntheory import discrete_log
>>> discrete_log(41, 15, 7)
3

References
==========

.. [1] https://mathworld.wolfram.com/DiscreteLogarithm.html
.. [2] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
    Vanstone, S. A. (1997).

r   )r   ré   r   r¼   iè  rZ   é
   l    J)£)rê   r   ré   r   r"   r   r#   rF   r$   r   rÔ   rø   r×   râ   r  )r'   r&   r¯   r.   Úprime_orderr   ré   r-   r/   ÚkxÚpyÚkyrû   r)   r*   rI   r�   s                    r2   r£   r£   ;  sØ  € ÷@ Ü�Q‹iœ ›¤F¨1£Iˆ!€Aàˆ1ƒuÜÐ/Ó0Ð0ØˆAƒvØà�}ð ˆÜ “l×(Ñ(Ö*‰FˆBØ�A‹vØ“=Ø“K 2¨¡6Ñ)”Kà"$ q¡&�G‘KÜ# B¨¡FÓ+×1Ñ1Ö3‘�Ø“=Ø“K 2Ñ%•Kà"$˜B“Kó	 4ñ +ð ˆØ—m‘m–o‰FˆBØ˜‘V‰OŠEñ &ð ˆØ—M‘M–O‰DˆAØˆAÜ˜1–X�Ü�q 1™* aÓ(¨AÓ-Ø‘K�EØ˜‘F’Aáñ ð �uØ#$¡5�Ó ñ $ð ÑÜ˜e“nˆàˆtƒ|Ü& q¨QÓ6Ð6Þ	ð ‰T‘#�a“&™™S ›V›Ñ$Ó%Ñ%©¨E«
°R©Ó7Ü/°°aÓ?Ð?Ø�]Ó"ä-¨a°AÓ=Ð=Ü(¨¨qÓ8Ð8ä'¨¨a¸ÓFÐFr4   c                óÜ  ^^• [        U 5      n [        T5      m[        U5      n[        T5      mTS::  a  [        S5      eU T-  n TT-  mUT-  nU S:X  a  [        TU* T5      $ TS:X  a9  / nUS:X  a  UR                  S5        TU-   S-  (       a  UR                  S5        U$ [	        SU -  T5      S:X  aO  [        U T5      nTU-  mX%-  nTS-  (       a  TT-  mTS-  m[        UU4S j[        TS-  U-
  T5       5       5      $ [        5       n[        TS-  SU -  U-  -
  SU -  T-  5       H3  n[        UT-
  SU -  5      u  p‰U	S:X  d  M  UR                  UT-  5        M5     [        U5      $ )a  
Find the solutions to `a x^2 + b x + c \equiv 0 \pmod{n}`.

Parameters
==========

a : int
b : int
c : int
n : int
    A positive integer.

Returns
=======

list[int] :
    A sorted list of solutions. If no solution exists, ``[]``.

Examples
========

>>> from sympy.ntheory.residue_ntheory import quadratic_congruence
>>> quadratic_congruence(2, 5, 3, 7) # 2x^2 + 5x + 3 = 0 (mod 7)
[2, 6]
>>> quadratic_congruence(8, 6, 4, 15) # No solution
[]

See Also
========

polynomial_congruence : Solve the polynomial congruence

r   r    r   r7   c              3  ó2   >#   • U  H  oT-
  T-  v •  M     g 7frO   rP   )r@   rI   r¯   r'   s     €€r2   rC   Ú'quadratic_congruence.<locals>.<genexpr>Ó  s   øé € ÐFÒ+E a˜1‘u –kÒ+Eùs   ƒrZ   )r   r"   r   r}   r   r   rs   rt   rf   rª   rg   )
r&   r¯   r¡   r'   ÚrootsÚinv_arŒ   rI   r_   Úrems
    ` `      r2   Úquadratic_congruencer  –  sw  ù€ ôD 	ˆq‹	€AÜˆq‹	€AÜˆq‹	€AÜˆq‹	€AØˆAƒvÜÐ@ÓAÐAØˆ�F€AØˆ�F€AØˆ�F€AàˆAƒvÜ   Q B¨Ó*Ð*ØˆAƒvàˆØ�‹6Ø�L‰L˜ŒOØ�‰E�Q�;Ø�L‰L˜ŒOØˆÜ
ˆ1ˆQ‰3�ƒ{�aÓÜ�q˜!“ˆØ	ˆU‰
ˆØ	‰
ˆØˆq�5Ø�‰FˆAØ	ˆa‰ˆÜÕF¬=¸¸A¹À¹À1Ô+EÓFÓFÐFÜ
‹%€CÜ˜1˜a™4 ! A¡# a¡%™<¨¨1©¨Q©Ö/ˆÜ˜˜A™˜q ™sÓ#‰ˆØ�!�8Ø�G‰G�A˜‘EŽNñ 0ô
 �#‹;Ðr4   c                óî   • U R                  5       (       d  [        S5      e[        U 5      nUR                  (       d  [        S5      eUR                  [
        :X  d  [        S5      eUR                  5       $ )zq
return coefficients of expr if it is a univariate polynomial
with integer coefficients else raise a ValueError.
z%The expression should be a polynomialz#The expression should be univariatez6The expression should should have integer coefficients)Úis_polynomialr"   r   Úis_univariater€   r   Ú
all_coeffs)ÚexprÚ
polynomials     r2   Ú_valid_exprr  Ý  sf   € ð ×Ñ×ÑÜÐ@ÓAÐAÜ�d“€JØ×#×#ÜÐ>Ó?Ð?Ø×Ñ¤Ó"ÜÐQÓRÐRØ× Ñ Ó"Ð"r4   c                ó*  • [        U 5      nU Vs/ s H  o3U-  PM	     nn[        U5      nUS:X  a  [        / UQUP76 $ US:X  a  [        S/UQUP76 $ US   S:X  a*  SUS   -   [        U5      :X  a  [	        US   * US-
  US5      $ [        X!5      $ s  snf )a«  
Find the solutions to a polynomial congruence equation modulo m.

Parameters
==========

expr : integer coefficient polynomial
m : positive integer

Examples
========

>>> from sympy.ntheory import polynomial_congruence
>>> from sympy.abc import x
>>> expr = x**6 - 2*x**5 -35
>>> polynomial_congruence(expr, 6125)
[3257]

See Also
========

sympy.polys.galoistools.gf_csolve : low level solving routine used by this routine

r6   r7   r   r   rk   T)r  r~   r  ÚsumrÃ   r   )r  rQ   ÚcoefficientsÚnumÚranks        r2   Úpolynomial_congruencer  í  s³   € ô2 ˜tÓ$€LÙ'3Ó4¢| ˜!”G¡|€LÐ4Üˆ|Ó€DØˆqƒyÜ#Ð5 \Ð5°1Ò5Ð5ØˆqƒyÜ# AÐ8¨Ð8°aÒ8Ð8Ø�A�˜!Ó  L°Ñ$4Ñ 4¼¸LÓ8IÓ IÜ˜L¨Ñ,Ð,¨d°Q©h¸¸4Ó@Ð@Ü�\Ó%Ð%ùò 5s   �Bc           
     ó8  • US:  a  [        S5      eU S:  d  US:  d  X:”  a  g[        U5      nUR                  5        VVs/ s H  u  pE[        XXE5      PM     nnn[	        UR                  5        VVs/ s H	  u  pGXG-  PM     snnUSS9S   $ s  snnf s  snnf )a  Compute ``binomial(n, m) % k``.

Explanation
===========

Returns ``binomial(n, m) % k`` using a generalization of Lucas'
Theorem for prime powers given by Granville [1]_, in conjunction with
the Chinese Remainder Theorem.  The residue for each prime power
is calculated in time O(log^2(n) + q^4*log(n)log(p) + q^4*p*log^3(p)).

Parameters
==========

n : an integer
m : an integer
k : a positive integer

Examples
========

>>> from sympy.ntheory.residue_ntheory import binomial_mod
>>> binomial_mod(10, 2, 6)  # binomial(10, 2) = 45
3
>>> binomial_mod(17, 9, 10)  # binomial(17, 9) = 24310
0

References
==========

.. [1] Binomial coefficients modulo prime powers, Andrew Granville,
    Available: https://web.archive.org/web/20170202003812/http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf
r   zk is required to be positiver   F)Úcheck)r"   r   r#   Ú_binomial_mod_prime_powerr   )r'   rQ   rK   Úfactorisationr)   r*   ÚresiduesrA   s           r2   Úbinomial_modr!    sš   € ðB 	ˆ1ƒu”JÐ=Ó>Ð>ð
 	ˆ1ƒu��A“˜› qÜ˜a“L€MØBO×BUÑBUÔBWÔXÒBW¹$¸!Ô)¨!°Ö5ÑBW€HÑXÜ =×#6Ñ#6Ô#8Ô9Ò#8™%˜!�”Ñ#8Ò9¸8È5ÑQÐRSÑTÐTùó YùÛ9s   ÁBÁ5B
c                ó,  ^^^^^^^• [        TT5      mUUU4S jmS mUUUU4S jm[        S/5      U4S j5       mUUUUU4S jnSnXX-
  p‡nS=n	=n
=p¼U(       aˆ  U" UT-  5      nU" UT-  5      U" UT-  5      -  T-  nUT-  [        UT5      [        UT5      snu  pu  nnUU-   U
-   T-  n
Xš-  n	UTS-
  :¼  a  Xº-  nX][        UT5      -  -  nUT-  nUS-  nU(       a  Mˆ  [        TS:X  a  TS	:¼  a  SOS
UT5      n[        TU	T5      U-  U-  T-  $ )ah  Compute ``binomial(n, m) % p**q`` for a prime ``p``.

Parameters
==========

n : positive integer
m : a nonnegative integer
p : a prime
q : a positive integer (the prime exponent)

Examples
========

>>> from sympy.ntheory.residue_ntheory import _binomial_mod_prime_power
>>> _binomial_mod_prime_power(10, 2, 3, 2)  # binomial(10, 2) = 45
0
>>> _binomial_mod_prime_power(17, 9, 2, 4)  # binomial(17, 9) = 24310
6

References
==========

.. [1] Binomial coefficients modulo prime powers, Andrew Granville,
    Available: https://web.archive.org/web/20170202003812/http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf
c                ó@  >• TS-  nS=p#US:X  a  TS:X  d  SU-  S-   TTT-  4;   a/  TS-  S:X  a  US-  n[        TSU-  5      [        TSU-  T-
  5      pTO&[        TSU-  S-   5      [        TSU-  S-   T-
  5      pT[        SUS-   5       HF  n[        US-
  T-  S-   UT-  5       H  nX'-  nX$-  nM     T
" XU5      nU[        X(U5      -  nX4-  nMH     TS:X  a:  U S-  n	[        SUS-   5       H  oiUS-  T
" XU5      -  -  n	M     U	S-  S:X  a  US-  nX4U-  -  nX4-  $ )zCompute (u*p)!_p modulo p^q.r7   r   rk   )r$   rF   )Úurx   ÚfacÚprodÚmoduloÚdivr–   ÚmulÚbj_Úsmrÿ   r)   r_   s             €€€r2   Úup_factorialÚ/_binomial_mod_prime_power.<locals>.up_factorial^  sY  ø€ à�‰FˆØˆˆØ�‹6�a˜1“f  !¡ a¡¨A¨q°©s¨8Ó 3Ø�1‰u˜‹z˜1 ™6˜1Ü˜a  1¡›+¤s¨1¨a°©c°A©g£‘Cä˜a  1¡ q¡›/¬3¨q°1°Q±3¸±7¸a±-Ó+@�CÜ�q˜!˜a™%–ˆAÜ˜a !™e Q™Y¨™]¨A¨a©CÖ0�Ø‘
�Ø‘’ñ 1ñ �Q˜1“+ˆCØ”C˜ &Ó)Ñ)ˆDØ‰NŠDñ !ð �‹6Ø�a‘ˆBÜ˜1˜a !™e–_�¨A¨q©D±2°a¸A³;Ñ,>Ñ&>¢b‘_Ø�A‰v˜‹{˜D B™J˜DØ˜‘ÑˆØ‰}Ðr4   c                ó²   • U n[        SUS-   5       H  nXA:w  d  M
  X0U -  XD-  -
  -  nM     [        SUS-   5       H  nXA:w  d  M
  X1U-  XD-  -
  -  nM     X1-  $ )z@Compute the exponent of (j*p)!_p in the calculation of (u*p)!_p.r   r‘   )r$  r–   rx   r&  rI   s        r2   rÿ   Ú%_binomial_mod_prime_power.<locals>.bju  sc   € àˆÜ�q˜!˜a™%–ˆAØ�v�t ™s Q¡S™yÑ(’tñ !ä�q˜!˜a™%–ˆAØ�v�t !¡ a¡c¡	Ñ)’tñ !à‰yÐr4   c                óh  >• Sn[        T	" U5      T
5      n[        ST5       HŒ  nUn[        UT-  S-   UT-  U-   S-   5       H  nXV-  nUT
-  nM     U n[        ST5       H  nX„:w  d  M
  XpU-
  -  nM     [        ST5       H  nX„:w  d  M
  XtU-
  -  nM     Xt-  nU[        XWT
5      -  nUT
-  nMŽ     U$ )z*Compute binomial(u*p + v, v)_p modulo p^q.r   )r   rF   r$   )r$  rJ   r&  r(  r–   r¯   Úv_rþ   rI   Ú	factorialr'  r)   r_   s            €€€€r2   Úup_plus_v_binomÚ2_binomial_mod_prime_power.<locals>.up_plus_v_binom~  sÏ   ø€ àˆÜ‘Y˜q“\ 6Ó*ˆÜ�q˜!–ˆAØˆAÜ˜A˜a™C !™G Q q¡S¨1¡W¨q¡[Ö1�Ø‘�Ø�V‘’ñ 2ð ˆBÜ˜1˜a–[�Ø•6˜2 Q¡™;š2ñ !ä˜1˜a–[�Ø•6˜2 a¡%™<š2ñ !à‰HˆBØ”C˜˜vÓ&Ñ&ˆDØ�F‰NŠDñ ð ˆr4   r   c                ó   >• XS   -  T-  $ )zCompute v! modulo p^q.rk   rP   )rJ   Úprevr'  s     €r2   r2  Ú,_binomial_mod_prime_power.<locals>.factorial‘  s   ø€ ð �b‘‰z˜FÑ"Ð"r4   c                óZ   >• [        U T5      u  pT" U5      T" U5      -  T" X5      -  T-  $ )zCompute n!_p modulo p^q.)rª   )r'   r$  rJ   r2  r'  r)   r,  r3  s      €€€€€r2   Úfactorial_pÚ._binomial_mod_prime_power.<locals>.factorial_p–  s2   ø€ ä�a˜‹|‰ˆÙ˜!“™|¨A›Ñ.±ÀÓ1FÑFÈ&ÑPÐPr4   r   r7   r6   rk   )r$   r   rª   r   )r'   rQ   r)   r_   r9  r&  ÚNjÚMjÚRjÚe0ÚcarryÚeq_1r–   Ú	numeratorÚdenominatorÚmjÚrjr)  rÿ   r2  r'  r,  r3  s     ``              @@@@@r2   r  r  >  sZ  þ€ ô< ��A‹Y€F÷ò.÷ð ô& �a�SÓô#ó ð#÷Qñ Qð
 €DØ�q‘uˆB€BàÐ€BÐˆÐ�Þ
Ù  V¡Ó,ˆ	Ù! " v¡+Ó.±¸RÀ&¹[Ó1IÑIÈFÑRˆØ!# Q¡¬¨r°1«´v¸bÀ!³}Ðˆ‰HˆR‘h�r˜2Ø�b‘˜5‘ QÑ&ˆØ
‰ˆØ��A‘‹:�t‘}�tØœF ;°Ó7Ñ7Ñ7ˆØ�‰ˆØ	ˆQ‰ˆ÷ ˆ"ô �1˜“6˜a 1›f‰a¨"¨d°FÓ
;€CÜ��2�vÓ Ñ$ tÑ+¨vÑ5Ð5r4   )T)F)Úreturnz	list[int]rO   )Nr  N)r'   r%   rå   r‡   )NN)KÚ
__future__r   Úsympy.external.gmpyr   r   r   r   r   r	   r
   Úsympy.polysr   Úsympy.polys.domainsr   Úsympy.polys.galoistoolsr   r   r   r   Ú	primetestr   Úgenerater   Úfactor_r   r   rú   r   Úsympy.utilities.decoratorr   Úsympy.utilities.memoizationr   Úsympy.utilities.miscr   Úsympy.utilities.iterablesr   Úsympy.core.randomr   r   Ú	itertoolsr   r3   rL   rU   rW   r`   r\   rq   ry   r%   rt   r{   r|   r—   rœ   rš   r¨   rº   rÃ   rÅ   rÉ   rÍ   rÐ   rÔ   r×   râ   ræ   rø   r  r£   r  r  r  r!  r  rP   r4   r2   Ú<module>rT     s  ðÝ "÷4÷ 4ñ 4å Ý "ß RÓ RÝ Ý  ß .Ý Ý 0Ý 7Ý 'Ý .ß /å òCòL=ò@*GòZ!ôH`òFXJòv:ôz*ðZ  #ô /8òdH#òVEòBXòv1ò<ò<;ò|Kô\Mô`ñ  ð BàØBñDñ*"ó	Dð*"ñZ ð ~àØBñDñ@ ó	Dð@ ñF ð pàØBñDñ/ó	Dð/ôd$+ôN*+ôZb?ôJô^.ôB-ô`WGòvDòN#ò "&òJ)UóXm6r4   