� I�g0A��p�dZddlZddlmZddlmZddlmZmZmZmZm Z m Z m Z dgdzZ e dd��D]Zegdd ez zze dezddedzz�<�dAd �Zd �Zd �ZedkrddlZejZejZd�Zedkrej��dkrd�Znd�Zd�e d��D��Zd�Zd�Zd�ZedkreZeZnedkr ejZeZeZneZeZedkrdee��vrejZd�e d��D��Zd�e d��D��Z d�Z!dZ"de"fd �Z#dde"fd!�Z$dde"fd"�Z%edkre%Z&ne$Z&dd#zZ'dd$zZ(dd%zZ)dd&zZ*d'Z+d(Z,d)�Z-d*�Z.d+�Z/d,�Z0d-�Z1e1Z2edkr9ej��dkrej3xZ4xZ5Z3ej6Z7n7ej8xZ4xZ5Z3ej7Z7n$edkre9ed.d/���xZ4xZ5Z3d0�Z7ne-Z4e.Z5e0Z3e/Z7ifd1�Z:d2Z;ddd3�fd4�Z<ddiddigfd5�Z=edkrej>Z<nedkr d6�Z<ej?Z:d7�Z@edkrd8�Z@d9ZAeBeA��ZCd:�ZDd;�ZEd<�ZFd=ZGde ifd>�ZHd?�ZId@�ZJdS)Bzw Utility functions for integer math. TODO: rename, cleanup, perhaps move the gmpy wrapper code here from settings.py �N)�bisect�)�xrange)�BACKEND�gmpy�sage� sage_utils�MPZ�MPZ_ONE�MPZ_ZERO����c�~�|g}|d||zkr!||d|zdzgz}|d||zk�!|ddd�S)a Return a list of integers ~= [start, n*start, ..., target/n^2, target/n, target] but conservatively rounded so that the quotient between two successive elements is actually slightly less than n. With n = 2, this describes suitable precision steps for a quadratically convergent algorithm such as Newton's method; with n = 3 steps for cubic convergence (Halley's method), etc. >>> giant_steps(50,1000) [66, 128, 253, 502, 1000] >>> giant_steps(50,1000,4) [65, 252, 1000] �����rN�)�start�target�n�Ls �g/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/mpmath/libmp/libintmath.py� giant_stepsrsV��& ��A� �B�%�%��'�/�/� ��2����A��� �� �B�%�%��'�/�/� �T�T�r�T�7�N�c�$�|dkr||z S|| zS)z�For an integer x, calculate x >> n with the fastest (floor) rounding. Unlike the plain Python expression (x >> n), n is allowed to be negative, in which case a left shift is performed.rr��xrs r�rshiftr+�!�� �A�v�v�a�1�f�}��Q�B�i�rc�$�|dkr||zS|| z S)z�For an integer x, calculate x << n. Unlike the plain Python expression (x << n), n is allowed to be negative, in which case a right shift with default (floor) rounding is performed.rrrs r�lshiftr!2rrrc��|sdS|dz}|r t|Sd}|dz}|dzs|dz}|dz }|dz�|t|dzzS)z1Count the number of trailing zero bits in abs(n).r��r)�small_trailing)r�low_byte�ts r�python_trailingr'>s{�� ���q��4�x�H��(��h�'�'� �A��!�G�A��$�h�� �a��� �Q����$�h�� �~�a�$�h�'� '�'rr�2c�L�|r!t|�����SdS�z<Count the number of trailing zero bits in abs(n) using gmpy.r)r � bit_scan1�rs r� gmpy_trailingr-Ns&��� ��Q���)�)�+�+�+��rc�L�|r!t|�����SdSr*)r �scan1r,s rr-r-Ss"��� ��Q�������'��rc��g|]}d|z��S)rr��.0�_s r� <listcomp>r4Ys�� #� #� #�1�!�Q�$� #� #� #r�,c��tt|��}|dkr|Sttj|d����dz }|t ||z zS)�0Calculate bit size of the nonnegative integer n.r5r�)r�powers�int�math�log�bctable)r�bcs r�python_bitcountr?[sN�� ��� � �B� �S�y�y�� � �T�X�a��^�^� � �q� �B� ���2��� �rc�N�|r"t|���d��SdS)r7rr)r � numdigitsr,s r� gmpy_bitcountrBcs(�����Q���!�!�!�$�$� $��rc�D�t|�����S�N)r �trailing_zero_bitsr,s r� sage_trailingrFls�� �q�6�6� $� $� &� &�&r� bit_lengthc�,�g|]}t|����Sr)�trailing�r2rs rr4r4~s�� .� .� .�a�h�q�k�k� .� .� .rc�,�g|]}t|����Sr)�bitcountrJs rr4r4s�� ,� ,� ,�1�8�A�;�;� ,� ,� ,ric�2�|t|��|zz|z S)zaChanges radix of a fixed-point number; i.e., converts x * 2**xbits to floor(x * 10**bdigits).�r )r�xbits�base�bdigitss r� bin_to_radixrR�s�� ��D� � �7�"� #�u� ,�,r�$0123456789abcdefghijklmnopqrstuvwxyz� c���|dkrt|��Sg}|r0t||��\}}|�||��|�0d�|ddd���S)ziReturn the string numeral of a positive integer in an arbitrary base. Most efficient for small input.rT�Nr)�str�divmod�append�join)rrP�digits�digs�digits r� small_numeralr^�st�� �r�z�z��1�v�v� � �D� �#��!�T�?�?���5� � � �F�5�M�"�"�"� �#� �7�7�4���"��:� � �rc�,�|dkr|sdSdt| |||��zS|dkrt|||��S|dz|dzz}t|||z��\}}t||||��}t||||���|d��}||zS)�_Represent the integer n as a string of digits in the given base. Recursive division is used to make this function about 3x faster than Python's str() for converting integers to decimal strings. The 'size' parameters specifies the number of digits in n; this number is only used to determine splitting points and need not be exact.r�0�-��rr)�numeralr^rX�rjust� rrP�sizer[�half�A�B�ad�bds r�numeral_pythonrm�s��� �A�v�v�� ��3��W�a�R��t�V�4�4�4�4� �c�z�z��Q��f�-�-�-� �A�I�$��(� #�D� �!�T�4�Z� � �D�A�q� ��D�$�� '� '�B� ��D�$�� '� '� -� -�d�C� 8� 8�B� ��7�Nrc�F�|dkrdt| |||��zS|dkrtj||��S|dz|dzz}t|t |��|z��\}}t||||��}t||||���|d��}||zS)r`rrbi`�rrra)rdrr[rXr rerfs r� numeral_gmpyro�s��� �1�u�u��W�a�R��t�V�4�4�4�4� �g�~�~��{�1�d�#�#�#� �A�I�$��(� #�D� �!�S��Y�Y��_� %� %�D�A�q� ��D�$�� '� '�B� ��D�$�� '� '� -� -�d�C� 8� 8�B� ��7�Nri iXi���ll c�*�|s|S|tkr6|tkrt|dz��St|dzdz��dz}n8t|��}|dz}t|d|zdz z dzdz��|dz z} |||zzdz }||kr|S|}�)zd Correctly (floor) rounded integer square root, using division. Fast up to ~200 digits. ��?g-�?rr�d�2)�_1_800�_1_50r:rL)r�rr>r�ys r�isqrt_small_pythonry�s��� �����6�z�z� �u�9�9��q�#�v�;�;� � ��3��)�)� *� *�Q� .��� �a�[�[�� ��E�� ��Q�q�S��W���#�A�%� &� &��2�� .��� �q�!�t�V�a�K�� ��6�6��H� �� rc�,�|tkrVt|dz��}|tkr7|||zzdz }|tkr!|||zzdz }|tkr |||zzdz }|St |��}d}|d|zz}|d|zz }||dzz }|dz}t d|��}tdd|zz||d|zz z dzz��}|}t||��D]1}||zd|z|z z } |||z z | z|z } |d|z| z z|dzz }|}�2|||z z||zz S) a Fast approximate integer square root, computed using division-free Newton iteration for large x. For random integers the result is almost always correct (floor(sqrt(x))), but is 1 ulp too small with a roughly 0.1% probability. If x is very close to an exact square, the answer is 1 ulp wrong with high probability. With 0 guard bits, the largest error over a set of 10^5 random inputs of size 1-10^5 bits was 3 ulp. The use of 10 guard bits almost certainly guarantees a max 1 ulp error. rrrrTrrtg@g��)rur:�_1_100�_1_200�_1_400rL�minr) rrxr>� guard_bits�hbc� startprecrw�pp�p�r2�xr2s r�isqrt_fast_pythonr��so��$ �6�z�z� ��3��K�K�� ��;�;��Q��T��a��A��F�{�{���A��X�!�O����;�;��Q��T��a��A��� �!���B��J��!�J�,��A��!�J�,��B��2�a�4�L�B� �a�%�C��B�� � �I� �C�!�I�+� �!��1�Y�;��"7�D�!@� @�A�A�A� �B� ��C� (� (������c�q��t�a�x� ���b��d� �r�!�a�'�� �1�a�4�3�,� �R��T� *�� ��� �q�#�v�J�A�j�L� )�)rc��|tkrt|��}||||zz fSt|��dz}|||zz }|dkr|dz}|dd|zzz }|dk�|r(|dd|zzkr|dz }|dd|zzz}|dd|zzk�||fS)z=Correctly rounded integer (floor) square root with remainder.rrr)�_1_600ryr�)rrx�rems r�sqrtrem_pythonr�s��� �6�z�z� �q� !� !���!�a��c�'�z���!���q� �A� �a��c�'�C� ��'�'� �Q��� ��!�A�#���� ��'�'� � ���1�Q�3��-�-��Q�����!�A�#������1�Q�3��-�-� �c�6�Mrc�,�t|��dS)z2Integer square root with correct (floor) rounding.r)r�)rs r� isqrt_pythonr�+s�� �!� � �Q� �rc�&�t||z��SrD)� isqrt_fast)r�precs r� sqrt_fixedr�/s�� �a��g� � �r�isqrtc�D�t|�����SrD)r r�r,s r�<lambda>r�=s��s�1�v�v�|�|�~�~�rc�D�t|�����SrD)r �sqrtremr,s rr�r�>s���A�����(�(�rc�D�|dkrd| dzzt| ��zS||vr||S|}ttttf\}}}}|rE|dzr!||z}||z|z||zz||z|z}}|dz}n||z}||z|z|d|z|zz}}|dz}|�E|dkr|||<|S)zCComputes the nth Fibonacci number as an integer, for integer n.rrrrrc)�ifibr r ) r�_cache�m�a�br��q�aq�qqs rr�r�Fs��� �1�u�u��q�b��d�|�d�A�2�h�h�&�&��F�{�{��a�y�� �A��(�H�g�5�J�A�q�!�Q� �� �q�5� ��1��B��Q�3�r�6�!�A�#�:�q��s�2�v�q�A� ��F�A�A��1��B��Q�3�r�6�2�a��c�!�e�8�q�A� �!�G�A� �� �3�w�w���q� � �Hri�)rrc��|�|��}|r|St|��}||dz }t}||kr||z}||kr|||<|dz }||k�|S)z.Return n factorial (for integers n >= 0 only).r)�get�len�MAX_FACTORIAL_CACHE)r�memo�f�kr��MAXs r�ifacr�asz�� ���� � �A����� �D� � �A� �Q�q�S� �A� �C� �q�&�&� �Q��� ��8�8��D��G� �Q��� �q�&�&� �Hrc���||dz}|�|��}|r|St|��}||}t}||kr|dz }||z}||kr|||<||k�|S)z4Return n!! (double factorial), integers n >= 0 only.rr)r��maxr�)r� memo_pairr�r�r�r�r�s r�ifac2r�ps��� �Q�q�S�>�D� ���� � �A����� �D� � �A� �Q��A� �C� �a�%�%� �Q��� �Q��� ��8�8��D��G� �a�%�%� �Hrc�D�ttj|����SrD)r:r� factorialr,s rr�r��s��S����*�*�+�+�rc��|dz}tt|����}ddg|dd�<tdt|dz��dz��D]&}||rt|dz||��D]}d||<��'d�|D��S)Nrrrrrc��g|]}|�|��Srr)r2r�s rr4zlist_primes.<locals>.<listcomp>�s�� "� "� "�!�� "�A� "� "� "r)�listrr:)r�sieve�i�js r� list_primesr��s��� �A��A� �����O�O�E��A��E�"�1�"�I� �A�s�1�c�6�{�{�1�}� %� %���� ��8� ��A�q�D�!�Q�'�'� � ����a���� "� "�u� "� "� "�"rc�D�d�tj|dz��D��S)Nc�,�g|]}t|����Sr)r:r1s rr4zlist_primes.<locals>.<listcomp>�s��1�1�1�1��A���1�1�1rr)r�primesr,s rr�r��s$��1�1�� �A�a�C� 0� 0�1�1�1�1r)r{�r� � ������%�)�+�/c�&�����t�����dzs�dkS�dkr �tvStD] }�|zsdS� �dz �t������z �����fd�}�dkrddg}n�dkrgd �}nt}|D]}||��sdS�d S) a& Determines whether n is a prime number. A probabilistic test is performed if n is very large. No special trick is used for detecting perfect powers. >>> sum(list_primes(100000)) 454396537 >>> sum(n*isprime(n) for n in range(100000)) 454396537 rrrtFc���t|����}|dks|�krdStd���D]}|dz�z}|�krdS�dS)NrTrF)�powr)r�rrw�dr�r�ss ����r�testzisprime.<locals>.test�sf��� ��!�A�J�J�� ��6�6�Q�!�V�V��4���!��� � �A��1��q��A��A�v�v��t�t���uri��r{l�He%�Z )rr{r�rr�r�r�T)r:�small_odd_primes_set�small_odd_primesrI)rr�r�� witnessesr�r�r�r�s` @@@r�isprimer��s ������ �A���A� �q�5���A�v� ��2�v�v��(�(�(� �����1�u� ��5�5� � �!��A��� � �A� �Q��A��������� �7�{�{��q�E� � � �_� � �&�&�&� � �$� � �����t�A�w�w� ��5�5� � �4rc� ��tt|����}|dkr|Sg}td|dz��D]B�|�zs;|�dzzsdSt�fd�|D����s|�����Cdt |��zS)z� Evaluates the Moebius function which is `mu(n) = (-1)^k` if `n` is a product of `k` distinct primes and `mu(n) = 0` otherwise. TODO: speed up using factorization rrrc3�"�K�|] }�|zV�� dSrDr)r2r�r�s �r� <genexpr>zmoebius.<locals>.<genexpr>�s'�����.�.��q�1�u�.�.�.�.�.�.rr)�absr:r�sumrYr�)r�factorsr�s @r�moebiusr��s���� �C��F�F� � �A��1�u�u����G� �A�q��s�^�^�"�"���A�� "���1��H� ��q�q��.�.�.�.�g�.�.�.�.�.� "����q�!�!�!�� ��W��� �rc�4�d}|D]}|r |r |||z}}|� �|}�|S)Nrr)�argsr�r�s r�gcdr��sL�� �A� ���� � �� ��!�a�%�1��� ���A�A� �Hri�c���|dzrtS|�|��}|r|St}|}d�dD��}td|dz��D]�}t|dzdd��D](}|dz ||z|dz||dzzz||dz<�)|�d��d}t|dzdd��D]*}|||dzz }||krd|dzz|d|zzz||<�+||krd|dzz|zd|zzcS��dS) a� Computes the Euler numbers `E(n)`, which can be defined as coefficients of the Taylor expansion of `1/cosh x`: .. math :: \frac{1}{\cosh x} = \sum_{n=0}^\infty \frac{E_n}{n!} x^n Example:: >>> [int(eulernum(n)) for n in range(11)] [1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521] >>> [int(eulernum(n)) for n in range(11)] # test cache [1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521] rc�,�g|]}t|����SrrNr1s rr4zeulernum.<locals>.<listcomp>s��'�'�'�A��Q���'�'�'r)rrrrrrr�����rrN)r r��MAX_EULER_CACHE�rangerY) r�r�r�r�rr�r��sumar�s r�eulernumr��s^��$ �1�u����� � �1� � �A����� �C� �A�'�'��'�'�'�A��A�q��s�m�m� /� /���q��s�B��#�#� /� /�A���c�1�Q�4�Z�1�Q�3��!�A�#��,�.�A�a��c�F�F� ���� � � ����q��s�B��#�#� :� :�A� �A�a��c�F�N�D��C�x�x� �A�q�D�\�D�A�q�D�L�9��q� �� ��6�6��1�a�4�L�$�&�!�Q�$�.� .� .� .� � /� /rc�p�|dks|dkrt�||krt||k��S|dkrtStg|dzz}t|d<t d|dz��D]A}t t ||��dd��D]}|dz ||z||dz z||<� �Bd||zz||zS)z, Stirling number of the first kind. rrrr)� ValueErrorr r r rr)rr�rr�r�s r� stirling1r�%s��� �1�u�u��A������A�v�v��1��6�{�{���1�u�u��� � �a��c��A� �A�a�D� �A�q��s�^�^�)�)����A�q� � �1�b�)�)� )� )�A��a�C�1�Q�4�<�!�A�a�C�&�(�A�a�D�D� )� �!�A�#�;��1�� �rc��|dks|dkrt�||krt||k��S|dkrt|dk��St}t}t |dz��D]I}||zdzr||t|��|zzz}n||t|��|zzz }|||z z|dzz}�J|t |��zS)z- Stirling number of the second kind. rr)r�r r r rr�)rr�r�r&r�s r� stirling2r�6s��� �1�u�u��A������A�v�v��1��6�{�{���A�v�v��1��6�{�{���A��A� �A�a�C�[�[�#�#�� ��E�Q�;� � ��S��V�V�Q�Y�� �A�A� ��S��V�V�Q�Y�� �A� ��Q��K�A��E� "��� ��Q���<�r)r)K�__doc__r;r�backendrrrrr r r r r$r�r�rrr!�operatorr'�versionr-r9r?rBrFrLrI� sage_bitcount�dirrG� trailtabler=rR� stddigitsr^rmrordrur�r~r}r|rvryr�r�r�r�� sqrt_fixed2r�� isqrt_smallr�� isqrt_remr��sqrt�getattrr�r�r�r��fac� fibonaccir�r��setr�r�r�r�r�r�r�r�rrr�<module>r�sJ���� � � � �������������L�L�L�L�L�L�L�L�L�L�L�L�L�L�L�L�L�L���s��� ��q����6�6�A�&'�S�A��!��H�%5�N�1�a�4�>��Q�q�S��>�"�"�����0 � � � � � � �f����O�O�O� �_�F� �_�F� (� (� (� �f����t�|�~�~���� � � � �  � � � $� #���c� � � #� #� #��������'�'�'� �f����H��H�H� �����'�M��H��H�H��H��H� �f������T���2�2���H�/� .�5�5��:�:� .� .� .� � ,� ,���d� � � ,� ,� ,��-�-�-� 3� ��Y� � � � ��A�i�����,�!�I�����, �f����G�G��G� �C��� �C��� �C��� �C��� �� �����4.*�.*�.*�`���( � � ����� � �f����t�|�~�~����+/�:�5� �5�j�5��.���+/�9�4� �4�j�5��,��� ������ �G�%=�%=�>�>�?�K�?�*�u�(�(�G�G�$�K�"�J� �E��G�� � � � �2������  �  �  �  ��1���!�u�~� � � � �  �f��� �8�D�D� ���� +� +�D� �>�D�#�#�#� �f���2�2�2�<���s�+�,�,��*�*�*�X���& � � �J���'�{�$/�$/�$/�$/�L���"����r
Memory