
    MZdW                         d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZ d dlmZ ddlmZmZmZmZ d dlZd Zd Zd Zdd	Zd
gZy)    )fzerofrom_intfrom_rationalfonefhalfbitcountto_intto_strmpf_mulmpf_divmpf_submpf_addmpf_sqrtmpf_pimpf_cosh_sinhmpf_cosmpf_sin)igcd   )_sqrt_mod_prime_powerlegendre_symboljacobi_symbolis_quad_residueNc                     d} dg| z  a dg| z  at        | dz        dz   }t        d|      D ]:  }t         |   dk(  st        ||z  | |      D ]  }t         |   dk(  s|t         |<    < t        d|       D ]d  }t         |   dk(  r|t         |<   |dz
  t        |<   %t         |   }||z  }||z  dk(  rt        |   |z  t        |<   Ot        |   |dz
  z  t        |<   f y )N順 r   r         ?      )_factor_totientintrange)maxnlimijxys         ;/usr/lib/python3/dist-packages/sympy/ntheory/partitions_.py_prer*   
   s    D c$hGs4xH
dCi.1
C1c] #1:?1Q3a( #1:?!"GAJ##
 1d^ 
.1:?GAJA#HQKAJqDq5A:"1+a-HQK"1+q1u-HQK
.    c                    |dk(  rt         S |}d}t        |   }||z  dk(  r||z  }|dz  }||z  dk(  r||z  }dd| z  z
  }t        |      }|dk(  r|dk(  rd|z  }	|	||	z  z   }|t        d|dz
  |	      z  |	z  }t	        |d|dz         d   }
t        t        t        d|
z        ||      t        |	      |      }t        t        t        d	|z  t        |
dz
  |
      z        t        t        |      |      |      t        ||      |      S |dk(  rd|z  }	|	||	z  z   }|dkD  r|t        d
|dz  dz
  |	      z  |	z  }t	        |d|dz         d   }
t        t        t        d|
z        ||      t        |	      |      }t        t        t        dd	|dz   z  z  t        |
d      z        t        t        |dz        |      |      t        ||      |      S |||z  z   }||z  dk(  r>|dk(  r3t        t        t        d|            t        t        |      |      |      S t        S t        ||      st        S ||dz
  z  |dz
  z  }|t        d|dz
  |      z  }t	        |||      d   }
t        t        t        d|
z        ||      t        |      |      }t        t        t        dt        d|      z        t        t        |      |      |      t        ||      |      S |dk7  s|dk\  rt        |d      t        |d      }}d||z  z  }||z  | z  |dz  dz
  |z  z   t        ||z  |z  |z  t         |   dz
  |      z  |z  }||z  | z  |dz  dz
  |z  z   t        ||z  |z  |z  t         |   dz
  |      z  |z  }t        t#        |||      t#        |||      |      S |dk(  ryd| z  dz   t        dt         |   dz
  |      z  |z  }d| dz
  |dz  dz
  dz  z
  |dz  z  dz  z   dz  }t        t        t        d	      t#        |||      |      t#        |||            S d| z  dz   t        dt         |   dz
  |      z  |z  }d| |dz  dz
  dz  z
  dz  z   dz  }t        t#        |||      t#        |||      |      S )z Compute the inner sum in HRR formula [1]_

    References
    ==========

    .. [1] https://msp.org/pjm/1956/6-1/pjm-v6-n1-p18-p.pdf

    r   r      r      	         @   i@  r          )r   r   r   powr   r   r   r   r   r   r   r   r   r   r   r   r    _a)nkpreck1epk2vpimodmarg_phid1d2n1n2s                    r)   r7   r7   "   s    	Av	
B	A
A
q&A+
q	Q q&A+ 
BB	BqDA	B	Qw6A#Ca#gA3q!a%%%,A%aAE215A'1r4)*23-?C7"qq1ua!889!d+T3 T"D* * 6A#Ca#gA1us2q!tax--4%aAE215A'(1Q3-T:t%C7B!a%=A)>>?!Q$.6 T"D* * AIq5A:Av]1a01Xa[$/7 7 Lq!$L1q5z1q5!s3q!$$!!Q*1-HQqSM2t,QK wQ}Q**+Xa[$'/ C& 	&
 	Avab"tB|BBK!tAvQR'"R
HRL1,b12578!tAvQR'"R
HRL1,b12578r"b$'BD)94@@AvsQwC"!1266"<AERUQYN*RU3q88A=wRLr2td$ r2t 	 Q37CHRL1,b1
1R	7B
qBEAI>!Q&
&!	+B2b"d#RB%5t<<r+   c           	         t        |      }t        |      }t        |||      }t        t        |       t	        dd|      |      }t        ||      }t        t        ||      |      \  }	}
t        t        ||      t        t        ||      |      |      }t        t        ||	      t        |
||      |      }t        ||      S )z
    Compute the sinh term in the outer sum of the HRR formula.
    The constants sqrt(2/3*pi) and sqrt(8) must be precomputed.
    r   r-   )r   r   r   r   r   r   r   r   )r8   r&   r:   sq23pisqrt8r@   abcchshDEs                r)   _drS   x   s    
 	A	B4 A]1b$7>ADA71a=$/FBDq!2&	.A 	2At 4d;A1a=r+   c                    t        |       } | dk  ry| dk  rg d|    S dt               vr
t                t        t        j                  d| z  dz  dz  z  t        j
                  d| z        z
  t        j
                  d	      z  d
z         t        j
                  d	d      z  }t        |dz  dz         x}}t        }t        dt        d| dz  z  dz               }|dkD  rt        d      t        t        t        dd|      |      t        |      |      }t        t        d      |      }t        d
|      D ]{  }	t        | |	|      }
t!        | |	|||      }t#        |t        |
|      |      }|r$t%        d|	d|t'        |
d	      t'        |d	             t)        t+        t-        |                  dz   }} t        t-        t#        |t.        |                  S )a  
    Calculate the partition function P(n), i.e. the number of ways that
    n can be written as a sum of positive integers.

    P(n) is computed using the Hardy-Ramanujan-Rademacher formula [1]_.


    The correctness of this implementation has been tested through $10^{10}$.

    Examples
    ========

    >>> from sympy.ntheory import npartitions
    >>> npartitions(25)
    1958

    References
    ==========

    .. [1] https://mathworld.wolfram.com/PartitionFunctionP.html

    r   r   )r   r   r   r0   r      r   r   g      @r   r1   
   r   g?d      gQ?r   zInput too bigr0   r.   stepof2   )r!   globalsr*   mathr@   logr   max
ValueErrorr   r   r   r   r   r"   r7   rS   r   printr
   r   absr	   r   )r8   verbosepbitsr:   r=   sMrJ   rK   qrL   ds               r)   npartitionsri      s   . 	AA1uAv!!$$	!1R#1	xx|$&'( ) 	QE 59s?##D1AAs43;?#$A5y))XmAq!4a8&)QGFXa[!$E1a[ 	*q!QKq!Q&Awq!}d+&!T1fQmVAr]C S^$r)	* vga-.//r+   ri   )F)mpmath.libmpr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   sympy.core.numbersr   residue_ntheoryr   r   r   r   r]   r*   r7   rS   ri   __all__ r+   r)   <module>ro      sW   @ @ @ @ @ $5 5 .0T=l$40l /r+   