
    \h                     h    S SK Jr  S SKJrJr  S SKJr  S SKrS SK	J
r
  SS jrS rS rS	 rS
 rS rg)    )Tuple)IntegerRational)SN)gcdc                    [        U [        5      (       d>  [        U [        [        45      (       a  [	        U 5      S:X  a	  [        U 6 n O[        S5      eU S::  a  [        S5      eU R                  5       u  p#US:X  a"  US:X  a  S Vs/ s H  n[        U5      PM     sn$ X#S-   :X  a  [        R                  U/$ [        U 5      u  pVUS:X  a  U$ UR                  UR                  p2US:X  a  [        X#5      nOAUS:X  a  [        X#5      nO/US	:X  a  [        X#5      nOUS
:X  a  [!        X#5      nO[        S5      eXW Vs/ s H  n[        U5      PM     sn-   $ s  snf s  snf )a  
Return the list of denominators of an Egyptian fraction
expansion [1]_ of the said rational `r`.

Parameters
==========

r : Rational or (p, q)
    a positive rational number, ``p/q``.
algorithm : { "Greedy", "Graham Jewett", "Takenouchi", "Golomb" }, optional
    Denotes the algorithm to be used (the default is "Greedy").

Examples
========

>>> from sympy import Rational
>>> from sympy.ntheory.egyptian_fraction import egyptian_fraction
>>> egyptian_fraction(Rational(3, 7))
[3, 11, 231]
>>> egyptian_fraction((3, 7), "Graham Jewett")
[7, 8, 9, 56, 57, 72, 3192]
>>> egyptian_fraction((3, 7), "Takenouchi")
[4, 7, 28]
>>> egyptian_fraction((3, 7), "Golomb")
[3, 15, 35]
>>> egyptian_fraction((11, 5), "Golomb")
[1, 2, 3, 4, 9, 234, 1118, 2580]

See Also
========

sympy.core.numbers.Rational

Notes
=====

Currently the following algorithms are supported:

1) Greedy Algorithm

   Also called the Fibonacci-Sylvester algorithm [2]_.
   At each step, extract the largest unit fraction less
   than the target and replace the target with the remainder.

   It has some distinct properties:

   a) Given `p/q` in lowest terms, generates an expansion of maximum
      length `p`. Even as the numerators get large, the number of
      terms is seldom more than a handful.

   b) Uses minimal memory.

   c) The terms can blow up (standard examples of this are 5/121 and
      31/311).  The denominator is at most squared at each step
      (doubly-exponential growth) and typically exhibits
      singly-exponential growth.

2) Graham Jewett Algorithm

   The algorithm suggested by the result of Graham and Jewett.
   Note that this has a tendency to blow up: the length of the
   resulting expansion is always ``2**(x/gcd(x, y)) - 1``.  See [3]_.

3) Takenouchi Algorithm

   The algorithm suggested by Takenouchi (1921).
   Differs from the Graham-Jewett algorithm only in the handling
   of duplicates.  See [3]_.

4) Golomb's Algorithm

   A method given by Golumb (1962), using modular arithmetic and
   inverses.  It yields the same results as a method using continued
   fractions proposed by Bleicher (1972).  See [4]_.

If the given rational is greater than or equal to 1, a greedy algorithm
of summing the harmonic sequence 1/1 + 1/2 + 1/3 + ... is used, taking
all the unit fractions of this sequence until adding one more would be
greater than the given number.  This list of denominators is prefixed
to the result from the requested algorithm used on the remainder.  For
example, if r is 8/3, using the Greedy algorithm, we get [1, 2, 3, 4,
5, 6, 7, 14, 420], where the beginning of the sequence, [1, 2, 3, 4, 5,
6, 7] is part of the harmonic sequence summing to 363/140, leaving a
remainder of 31/420, which yields [14, 420] by the Greedy algorithm.
The result of egyptian_fraction(Rational(8, 3), "Golomb") is [1, 2, 3,
4, 5, 6, 7, 14, 574, 2788, 6460, 11590, 33062, 113820], and so on.

References
==========

.. [1] https://en.wikipedia.org/wiki/Egyptian_fraction
.. [2] https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions
.. [3] https://www.ics.uci.edu/~eppstein/numth/egypt/conflict.html
.. [4] https://web.archive.org/web/20180413004012/https://ami.ektf.hu/uploads/papers/finalpdf/AMI_42_from129to134.pdf

   z)Value must be a Rational or tuple of intsr   zValue must be positive   )r
   r	         GreedyzGraham Jewett
TakenouchiGolombzEntered invalid algorithm)
isinstancer   r   tuplelen
ValueErroras_numer_denomr   r   Oneegypt_harmonicpqegypt_greedyegypt_graham_jewettegypt_takenouchiegypt_golomb)r	algorithmxyiprefixrempostfixs           W/var/www/auris/envauris/lib/python3.13/site-packages/sympy/ntheory/egyptian_fraction.pyegyptian_fractionr&   	   sI   D a""a%((SVq[!AHIIAv122 DAAv!q&$01Lq
L11Ezqz #KF
ax55#%%q Hq$	o	%%a+	l	""1(	h	q$4551AWQZ111+ 2* 2s   E<Ec                     U S:X  a  U/$ U* U -  nXU -  S-   -  n[        X#5      nUS:  a  X$-  X4-  peOX#peX-  S-   /[        XV5      -   $ Nr
   )r   r   )r   r    abcnumdenoms          r%   r   r      sb    Avs
R1H!taxLIq5qtqzL444    c                 v   U/U -  n[        U5      [        [        U5      5      :w  a  UR                  5         [        [        U5      S-
  5       H  nX#   X#S-      :X  d  M    O   UW   S-   X#S-   '   UR	                  X#   X#   S-   -  5        [        U5      [        [        U5      5      :w  a  M  [        U5      $ r(   )r   setsortrangeappendsorted)r   r    lr!   s       r%   r   r      s    	
aA a&CAK
	s1vz"AtqQx # Q4!8a%	qtax! a&CAK
 !9r.   c                    U S:X  a'  US-  S:X  a  US-  U/$ US-
  S-  nUS-   nX2-   nX4X4-  /$ U/U -  n[        U5      [        [        U5      5      :w  a  UR                  5         [        [        U5      S-
  5       H  nXR   XRS-      :X  d  M    O   UW   nUS-  S:X  a  XR   S-  XR'   XRS-   	 OUS-   S-  XDS-   -  S-  sXR'   XRS-   '   [        U5      [        [        U5      5      :w  a  M  [	        U5      $ )Nr   r	   r   r
   )r   r0   r1   r2   r4   )r   r    r!   jkr5   s         r%   r   r      s    	Avq5A:qD!9UQJEEac{	
aA
a&CAK
	s1vz"AtqQx # aDq5A:419ADa%!eaZEANAD!E( a&CAK
 !9r.   c                     U S:X  a  U/$ [         R                  R                  R                  [	        U 5      [	        U5      5      nX!-  /nUR                  [        X-  S-
  U-  U5      5        [        U5      $ r(   )sympypolysZZinvertintextendr   r4   )r   r    xprvs       r%   r   r      sb    Avs
			s1vs1v	.B
$BIIlAD1Hq="-.":r.   c                     / n[         R                  n[         R                  nUSU-  -   U ::  a,  USU-  -  nUR                  U5        US-  nUSU-  -   U ::  a  M,  XU-
  4$ r(   )r   r   Zeror3   )r   rA   daccs       r%   r   r      sh    	B	A
&&C
!)q.qs

		!	Q !)q. C=r.   )r   )sympy.core.containersr   sympy.core.numbersr   r   sympy.core.singletonr   sympy.polysr:   mathr   r&   r   r   r   r   r    r.   r%   <module>rL      s4    ' 2 "  B2J5,2	r.   