
    \h7                         S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJ	r
JrJrJrJrJr  SSKJrJr  SS	 jrS
 rS r\" S5      S 5       r\R                  rS rS rS rS rS rS rg)ad  
The routines here were removed from numbers.py, power.py,
digits.py and factor_.py so they could be imported into core
without raising circular import errors.

Although the name 'intfunc' was chosen to represent functions that
work with integers, it can also be thought of as containing
internal/core functions that are needed by the classes of the core.
    N)	lru_cache   )sympify)S)gcdlcmsqrtiroot	bit_scan1gcdext)as_int
filldedentc                 l    US:  a  [        S5      eU (       d  g[        [        U 5      U5      u  p#SU-   $ )a  Return the number of digits needed to express n in give base.

Examples
========

>>> from sympy.core.intfunc import num_digits
>>> num_digits(10)
2
>>> num_digits(10, 2)  # 1010 -> 4 digits
4
>>> num_digits(-100, 16)  # -64 -> 2 digits
2


Parameters
==========

n: integer
    The number whose digits are counted.

b: integer
    The base in which digits are computed.

See Also
========
sympy.ntheory.digits.digits, sympy.ntheory.digits.count_digits
r   zbase must be int greater than 1r   )
ValueErrorinteger_logabs)nbaseets       J/var/www/auris/envauris/lib/python3.13/site-packages/sympy/core/intfunc.py
num_digitsr      s8    8 ax:;;s1vt$DAq5L    c                    [        U 5      n [        U5      nUS:  a/  [        [        U 5      U* 5      u  p#U=(       a    US-  U S:  :H  nX#4$ US::  a  [        S5      eU S:  a  US:w  a  g[        U * U5      u  p#US4$ U S:X  a  [        S5      eX:  a  SU S:H  4$ US:X  a#  U R	                  5       S-
  nU[        U 5      U:H  4$ [        U5      nSU-  U:X  a,  [        U R	                  5       S-
  5      U-  nSX2-  -  nX$U :H  4$ [        R                  " [        R                  " U 5      [        R                  " U5      -  5      nX-  nX@::  a  US-  nXA-  nX@::  a  M  XTU :  -
  X@:H  =(       d    XA-  U :H  4$ )aG  
Returns ``(e, bool)`` where e is the largest nonnegative integer
such that :math:`|n| \geq |b^e|` and ``bool`` is True if $n = b^e$.

Examples
========

>>> from sympy import integer_log
>>> integer_log(125, 5)
(3, True)
>>> integer_log(17, 9)
(1, False)

If the base is positive and the number negative the
return value will always be the same except for 2:

>>> integer_log(-4, 2)
(2, False)
>>> integer_log(-16, 4)
(0, False)

When the base is negative, the returned value
will only be True if the parity of the exponent is
correct for the sign of the base:

>>> integer_log(4, -2)
(2, True)
>>> integer_log(8, -2)
(3, False)
>>> integer_log(-8, -2)
(3, True)
>>> integer_log(-4, -2)
(2, False)

See Also
========
integer_nthroot
sympy.ntheory.primetest.is_square
sympy.ntheory.factor_.multiplicity
sympy.ntheory.factor_.perfect_power
r      r   zbase must be 2 or more)r   FFzn cannot be 0)
r   r   r   r   
bit_lengthtrailingintmathfloorlog10)r   br   r   n_ds         r   r   r   :   s   T 	q	Aq	A1u3q6A2& "!a%AE"tAv1221u6A2q!%xAv))u!q&yAvLLNQ(1+"""A!tqy"#Q&13Z'z

4::a=4::a=01A	
B
'	Q
 ' Q<"'/RUaZ00r   c                 :    U (       d  g[        [        U 5      5      $ )a  Count the number of trailing zero digits in the binary
representation of n, i.e. determine the largest power of 2
that divides n.

Examples
========

>>> from sympy import trailing
>>> trailing(128)
7
>>> trailing(63)
0

See Also
========
sympy.ntheory.factor_.multiplicity

r   )r   r   r   s    r   r   r      s    & SVr   i   c                      [        U 5      S:  a  [        S[        U 5      -  5      e[        [        [	        [
        U 5      6 5      $ )a  Computes nonnegative integer greatest common divisor.

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

The algorithm is based on the well known Euclid's algorithm [1]_. To
improve speed, ``igcd()`` has its own caching mechanism.
If you do not need the cache mechanism, using ``sympy.external.gmpy.gcd``.

Examples
========

>>> from sympy import igcd
>>> igcd(2, 4)
2
>>> igcd(5, 10, 15)
5

References
==========

.. [1] https://en.wikipedia.org/wiki/Euclidean_algorithm

r   z,igcd() takes at least 2 arguments (%s given))len	TypeErrorr   
number_gcdmapr   argss    r   igcdr.      s;    4 4y1}FTRSSz3vt,-..r   c                    [        [        U 5      5      [        [        U5      5      pX:  a  XpS[        R                  R                  -  nU R                  5       U:  a  US:w  a  U R                  5       U-
  n[        X-	  5      [        X-	  5      pTSu  pgp XX-   S::  a  OfXF-   XX-   -  n
XJU-  -
  XzU	-  -
  pX-   S:  a  OGX[pTXXjU-  -
  U4u  pgpXY-   S::  a  O/XG-   XY-   -  n
XJU-  -
  XjU-  -
  pX-   S:  a  OX[pTXXX-  -
  4u  pgpMo  US:X  a  XU-  pM  X`-  Xq-  -   X-  X-  -   pU R                  5       U:  a  US:w  a  M  U(       a  XU-  pU(       a  M  U $ )ac  Computes greatest common divisor of two integers.

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

Euclid's algorithm for the computation of the greatest
common divisor ``gcd(a, b)``  of two (positive) integers
$a$ and $b$ is based on the division identity
$$ a = q \times b + r$$,
where the quotient  $q$  and the remainder  $r$  are integers
and  $0 \le r < b$. Then each common divisor of  $a$  and  $b$
divides  $r$, and it follows that  ``gcd(a, b) == gcd(b, r)``.
The algorithm works by constructing the sequence
r0, r1, r2, ..., where  r0 = a, r1 = b,  and each  rn
is the remainder from the division of the two preceding
elements.

In Python, ``q = a // b``  and  ``r = a % b``  are obtained by the
floor division and the remainder operations, respectively.
These are the most expensive arithmetic operations, especially
for large  a  and  b.

Lehmer's algorithm [1]_ is based on the observation that the quotients
``qn = r(n-1) // rn``  are in general small integers even
when  a  and  b  are very large. Hence the quotients can be
usually determined from a relatively small number of most
significant bits.

The efficiency of the algorithm is further enhanced by not
computing each long remainder in Euclid's sequence. The remainders
are linear combinations of  a  and  b  with integer coefficients
derived from the quotients. The coefficients can be computed
as far as the quotients can be determined from the chosen
most significant parts of  a  and  b. Only then a new pair of
consecutive remainders is computed and the algorithm starts
anew with this pair.

References
==========

.. [1] https://en.wikipedia.org/wiki/Lehmer%27s_GCD_algorithm

r   r   )r   r   r   r   )r   r   sysint_infobits_per_digitr   r   )ar"   nbitsr   xyABCDqx_qyB_qDA_qCs                 r   igcd_lehmerr?      s   X vay>3vay>qu1 +++E
,,.5
 Q!V LLNU"16{CK1  
a6 uzAE"A
 UAAI${Q qqq5y$.JA! uzAE"AUAAI${QqtY.JA!q z 6 !eq uqu}aeaem1e ,,.5
 Q!Vj a%1 ! Hr   c                      [        U 5      S:  a  [        S[        U 5      -  5      e[        [        [	        [
        U 5      6 5      $ )zComputes integer least common multiple.

Examples
========

>>> from sympy import ilcm
>>> ilcm(5, 10)
10
>>> ilcm(7, 3)
21
>>> ilcm(5, 10, 15)
30

r   z,ilcm() takes at least 2 arguments (%s given))r(   r)   r   
number_lcmr+   r   r,   s    r   ilcmrB   V  s;     4y1}FTRSSz3vt,-..r   c                 L    [        [        U 5      [        U5      5      u  p#nX4U4$ )zReturns x, y, g such that g = x*a + y*b = gcd(a, b).

Examples
========

>>> from sympy.core.intfunc import igcdex
>>> igcdex(2, 3)
(-1, 1, 1)
>>> igcdex(10, 12)
(-1, 1, 2)

>>> x, y, g = igcdex(100, 2004)
>>> x, y, g
(-20, 1, 4)
>>> x*100 + y*2004
4

)r   r   )r3   r"   gr5   r6   s        r   igcdexrE   j  s%    & SVSV$GA!7Nr   c                    Sn [        U 5      [        U5      pUS:w  a  US:w  a  [        X5      u  p4nUS:X  a  X1-  nUc  [        SU < SU< S35      eU$ ! [         a    [        U 5      [        U5      pU R                  (       a  UR                  (       d  [        [        S5      5      eUS:  nU[        R                  [        R                  4;  a  [        SU-  5      eU(       a  SU -  n Nf = f)	a  
Return the number $c$ such that, $a \times c = 1 \pmod{m}$
where $c$ has the same sign as $m$. If no such value exists,
a ValueError is raised.

Examples
========

>>> from sympy import mod_inverse, S

Suppose we wish to find multiplicative inverse $x$ of
3 modulo 11. This is the same as finding $x$ such
that $3x = 1 \pmod{11}$. One value of x that satisfies
this congruence is 4. Because $3 \times 4 = 12$ and $12 = 1 \pmod{11}$.
This is the value returned by ``mod_inverse``:

>>> mod_inverse(3, 11)
4
>>> mod_inverse(-3, 11)
7

When there is a common factor between the numerators of
`a` and `m` the inverse does not exist:

>>> mod_inverse(2, 4)
Traceback (most recent call last):
...
ValueError: inverse of 2 mod 4 does not exist

>>> mod_inverse(S(2)/7, S(5)/2)
7/2

References
==========

.. [1] https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
.. [2] https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
Nr   z
                Expected numbers for arguments; symbolic `mod_inverse`
                is not implemented
                but symbolic expressions can be handled with the
                similar function,
                sympy.polys.polytools.invertz*m > 1 did not evaluate; try to simplify %szinverse of z (mod z) does not exist)
r   rE   r   r   	is_numberr)   r   r   truefalse)r3   mcr5   _rD   bigs          r   mod_inverserO     s    N 	Aay&)16a2gQlGA!AvE& 	yAqIJJH)  qz71:10	 	 !eqvvqww''IAMNNAA#s   9A BC10C1c                 ^    U S:  a  [        S5      e[        [        [        U 5      5      5      $ )a  Return the largest integer less than or equal to `\sqrt{n}`.

Parameters
==========

n : non-negative integer

Returns
=======

int : `\left\lfloor\sqrt{n}\right\rfloor`

Raises
======

ValueError
    If n is negative.
TypeError
    If n is of a type that cannot be compared to ``int``.
    Therefore, a TypeError is raised for ``str``, but not for ``float``.

Examples
========

>>> from sympy.core.intfunc import isqrt
>>> isqrt(0)
0
>>> isqrt(9)
3
>>> isqrt(10)
3
>>> isqrt("30")
Traceback (most recent call last):
    ...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> from sympy.core.numbers import Rational
>>> isqrt(Rational(-1, 2))
Traceback (most recent call last):
    ...
ValueError: n must be nonnegative

r   zn must be nonnegative)r   r   r	   r&   s    r   isqrtrQ     s+    V 	1u011tCF|r   c                 \    [        [        U 5      [        U5      5      u  p#[        U5      U4$ )a  
Return a tuple containing x = floor(y**(1/n))
and a boolean indicating whether the result is exact (that is,
whether x**n == y).

Examples
========

>>> from sympy import integer_nthroot
>>> integer_nthroot(16, 2)
(4, True)
>>> integer_nthroot(26, 2)
(5, False)

To simply determine if a number is a perfect square, the is_square
function should be used:

>>> from sympy.ntheory.primetest import is_square
>>> is_square(26)
False

See Also
========
sympy.ntheory.primetest.is_square
integer_log
)r
   r   r   )r6   r   r5   r"   s       r   integer_nthrootrS     s(    6 F1I&DAq619r   )
   )__doc__r   r0   	functoolsr   r   	singletonr   sympy.external.gmpyr   r*   r   rA   r	   r
   r   r   sympy.utilities.miscr   r   r   r   r   r.   igcd2r?   rB   rE   rO   rQ   rS    r   r   <module>r\      s     
   ; ; 3!HM1`0 4/ /< 	Od/(.BJ-`r   