U
    9%eU                     @   sh   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G dd deZ
d	d
 Zdd Zdd ZdS )z
The Schur number S(k) is the largest integer n for which the interval [1,n]
can be partitioned into k sum-free sets.(https://mathworld.wolfram.com/SchurNumber.html)
    N)S)Basic)Function)Integerc                   @   s$   e Zd ZdZedd Zdd ZdS )SchurNumbera\  
    This function creates a SchurNumber object
    which is evaluated for `k \le 5` otherwise only
    the lower bound information can be retrieved.

    Examples
    ========

    >>> from sympy.combinatorics.schur_number import SchurNumber

    Since S(3) = 13, hence the output is a number
    >>> SchurNumber(3)
    13

    We do not know the Schur number for values greater than 5, hence
    only the object is returned
    >>> SchurNumber(6)
    SchurNumber(6)

    Now, the lower bound information can be retrieved using lower_bound()
    method
    >>> SchurNumber(6).lower_bound()
    536

    c                 C   s^   |j rZ|tjkrtjS |jr"tjS |jr.|jr6tddddddd}|dkrZt|| S d S )	Nzk should be a positive integer         ,      )r         r      r   )		is_Numberr   Infinityis_zeroZZero
is_integerZis_negative
ValueErrorr   )clskZfirst_known_schur_numbers r   _/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/combinatorics/schur_number.pyeval'   s    
zSchurNumber.evalc                 C   sZ   | j d }|dkrtdS |dkr*tdS |jrJd| |d   d S d| d d S )	Nr      i     i  r   r   r   )argsr   Z
is_Integerfunclower_bound)selfZf_r   r   r   r   4   s    
zSchurNumber.lower_boundN)__name__
__module____qualname____doc__classmethodr   r   r   r   r   r   r      s   
r   c                 C   sT   | t jkrtd| dkr$tdn(| dkr2d}nttd|  d d}t|S )NzInput must be finiter   z&n must be a non-zero positive integer.r   r   r   )r   r   r   mathceillogr   )nZmin_kr   r   r   _schur_subsets_numberA   s    

r(   c                 C   s   t | tr| jstdt| }| dkr2dgg}n:| dkrFddgg}n&| dkr\dddgg}nddgddgg}t||k rt|| }dd tt|| d d d D }|d  |7  < ql|S )	a  

    This function returns the partition in the minimum number of sum-free subsets
    according to the lower bound given by the Schur Number.

    Parameters
    ==========

    n: a number
        n is the upper limit of the range [1, n] for which we need to find and
        return the minimum number of free subsets according to the lower bound
        of schur number

    Returns
    =======

    List of lists
        List of the minimum number of sum-free subsets

    Notes
    =====

    It is possible for some n to make the partition into less
    subsets since the only known Schur numbers are:
    S(1) = 1, S(2) = 4, S(3) = 13, S(4) = 44.
    e.g for n = 44 the lower bound from the function above is 5 subsets but it has been proven
    that can be done with 4 subsets.

    Examples
    ========

    For n = 1, 2, 3 the answer is the set itself

    >>> from sympy.combinatorics.schur_number import schur_partition
    >>> schur_partition(2)
    [[1, 2]]

    For n > 3, the answer is the minimum number of sum-free subsets:

    >>> schur_partition(5)
    [[3, 2], [5], [1, 4]]

    >>> schur_partition(8)
    [[3, 2], [6, 5, 8], [1, 4, 7]]
    zInput value must be a numberr   r   r   r   c                 S   s   g | ]}d | d qS r   r   r   .0r   r   r   r   
<listcomp>   s     z#schur_partition.<locals>.<listcomp>)
isinstancer   r   r   r(   len_generate_next_listrange)r'   Znumber_of_subsetsZsum_free_subsetsZmissed_elementsr   r   r   schur_partitionO   s    /

$r2   c                    st   g }| D ]:} fdd|D } fdd|D }|| }| | q fddtt| d D }| | |} | S )Nc                    s    g | ]}|d   kr|d  qS )r   r   r+   numberr'   r   r   r,      s      z'_generate_next_list.<locals>.<listcomp>c                    s(   g | ] }|d  d  kr|d  d qS r)   r   r3   r5   r   r   r,      s      c                    s(   g | ] }d | d  krd | d qS r)   r   r*   r5   r   r   r,      s      r   )appendr1   r/   )Zcurrent_listr'   Znew_listitemZtemp_1Ztemp_2Znew_itemZ	last_listr   r5   r   r0      s    
r0   )r"   r$   Z
sympy.corer   Zsympy.core.basicr   Zsympy.core.functionr   Zsympy.core.numbersr   r   r(   r2   r0   r   r   r   r   <module>   s   5D