mò
ógÈFc        
   @   s€   d  Z  d „  Z d „  Z d „  Z d „  Z d Z d Z d „  Z d „  Z d „  Z	 d „  Z
 d e f d „  ƒ  YZ e ƒ  Z [ d S(   sC  Simple tools for manipulating types linear over the positive integers.

Example linear spaces over the positive integers:
 the natural numbers
 the integers, whether modulo some value or not
 the polynomials with integer coefficients
 the rational, real or complex numbers
 any vector or linear space over any of the above
c   
      C   sŒ  d } g  d } } |  | | | | } }	 } yÐ xÉ | oÁ t	 | |	 ƒ \ } } d | j  o" | j  o |	 j o
 d j n p t ‚ | o | i | |	 | f ƒ n | | j o | |	 |	 j  o |	 | j  p t ‚ | | |	 |	 } }	 } q5 WWn) t j
 o t d |  | | f ‚ n XxV | oN | i ƒ  \ } }	 } t	 | | |	 | |	 ƒ \ } } | d j p t ‚ q.W| | S(   s„   Division modulo a base.

    Returns a q for which num - q*den is a multiple of base, if possible: raises
    ValueError on failure.sG  
    First reduce num and den mod base.
    If num is 0, q is 0; if den is 0, we have ValueError.
    Thereafter, to solve for n-q*d = s*b:

        wlog, q = q%b and s = s%d
        rearrange to n%d - s * (b%d) = q*d
        i.e. s = (n%d) / (b%d) mod d
        use this in n - s*b = q*d to discover q = n / d mod b

    This may be implemented recursively as
        q, r = divmod(n - dividemod(n%d, b%d, d) * b, d)
        assert r is 0
        return q

    However, we can unroll the recursion to a pair of while loops.
    Compare and contrast with Euclid's algorithm (below). i    s   %d / %d mod %dN(   t   __algorithmt   stackt   qt   numt   baset   dent   nt   dt   bt   divmodt   rt   AssertionErrort   appendt   ZeroDivisionErrort
   ValueErrort   pop(
   R   R   R   R   R    R   R   R
   R   R   (    (    t)   /home/eddy/.sys/py/study/maths/natural.pyt	   dividemod   s*      = 2! !c         C   so   | d j  o | } n |  d j  o |  }  n |  d j o | Sn x# | d j o | |  | }  } qH W|  S(   s  Pair-wise highest common factor.

    The value returned is, strictly, that value whose set of factors is the
    intersection of the sets of factors of the two arguments, ignoring all
    universal factors (e.g. 1, -1: values which are factors of everything).
i    N(   R   t   a(   R   R   (    (    R   t   gcd:   s          c          G   s*   d } x |  D] } t | | ƒ } q W| S(   s¯  The highest natural common factor of its arguments.

    All arguments should be members of a linear space over the natural numbers,
    eg (optionally long) integers or polynomials with such coefficients; there
    may be arbitrarily many arguments.

    Formally, the % operator, as defined for the given arguments, must be
    guaranteed to yield a positive or zero value whenever its (two) arguments
    are positive.  This is true for integers.

    Formally, at least one argument (to hcf) must be non-zero: if (there are no
    arguments, or) the arguments are all zero, of which every value is a factor,
    there is no highest common factor - all integers are factors of 0.  In this
    case, the value zero is returned.

    To justify not raising an error when (there are no inputs, or) all inputs
    are zero, we can re-cast the definition as: `that non-negative value which
    has, as its factors, exactly those naturals which are factors of all the
    arguments' (reading 'is n a factor of all arguments' as the negation of 'no
    argument does not have n as a factor' for the case of no arguments).  So the
    result must be a multiple of every common factor of the arguments, but of
    nothing else.  This coincides with `highest common factor' when at least one
    argument is non-zero: and gives zero when all arguments are zero.  Note that
    -1 is a factor of every value (just as is 1), hence the need to specify
    `non-negative'.

    With this (undeniably less catchy) re-definition, we also get: a
    concatenation of lists of values has, as its hcf, the hcf of the values
    obtained by taking the hcfs of the lists seperately; i.e. hcf is a
    transitive binary operator.
i    N(   t   thist   otherst   otherR   (   R   R   R   (    (    R   t   hcfL   s       c          G   s]   d } x: |  D]2 } | p | Sn t | | ƒ } | | | } q W| d j  o	 | Sn | S(   s  The smallest common multiple of its arguments.

    All arguments should be members of a linear space over the natural numbers,
    e.g. (optionally long) integers or polynomials with such coefficients.
    There may be arbitrarily many arguments.

    If any entry is zero, so is the result: zero is a multiple of everything,
    and is smaller than any other value; furthermore, nothing else is a multiple
    of zero, so it is the only candidate.  The return when no arguments are
    supplied is 1, to ensure that lcm is transitive.
i   i    N(   R   R   R   R   t   c(   R   R   R   R   (    (    R   t   lcmp   s    
    	s»  Any rational whose square is an integer is, itself, an integer.

As a special case, this tells us that the square root of 2 is irrational.

Proof:

  Suppose, for positive integers p, q, that the square of p/q is an integer, n.
  Thus p.p = q.q.n and n is positive.  There are positive integers m, r for
  which m.r.r = n and m has no perfect square as a factor, yielding p.p =
  (q.r).(q.r).m.

  Expressing p, q.r and m in terms of their prime factors we now find that every
  prime factor of m has odd multiplicity as a factor of p.p, all of whose
  factors have even multiplicity; thus m cannot have any prime factors, so m is
  1 and p = q.r has q as a factor so p/q = r is a positive integer. i   i   i   i   i   i   i   i   i   i   c         C   s€   x5 t  D]- } t |  | ƒ \ } } | d j o Pq q Wd } x; | d j o- |  | d j o | | 7} n | d 8} qA W| S(   sg   Returns the sum of the proper factors of N.

    Thus N is perfect precisely if N == factorsum(N).
    i    i   N(   t   _early_primest   pR	   t   Nt   iR
   t   S(   R   R   R   R   R
   (    (    R   t	   factorsum—   s       	 c          c   s.  d }  x!t ox5 t D]- } t |  | ƒ \ } } | d j o Pq q Wd } xH | d j o: | |  j o- |  | d j o | | 7} n | d 8} qQ W|  | j ou |  Vd } x |  d | >@p | d 7} q³ W|  | ?d d | >d j o) d Gt	 |  ƒ G| Gt	 |  | d ?ƒ GHqn |  d 7}  q	 Wd S(   sÕ  Returns an iterator over the perfect numbers.

    All known perfect numbers are 2**n * (2**(1+n) -1) for some positive natural
    n (for which the second factor, 2**(1+n) -1, is prime), so iterating over
    these (which is *much* quicker, even pausing to check primality of the
    second factor) shall (probably) yield the same result.  The iterator yielded
    by this implementation checks for other perfect numbers and prints a message
    if it ever finds one.
i   i    l    s   Unusually perfectN(
   R   t   TrueR   R   R	   t   jR
   R   R   t   hex(   R   R!   R   R   R   R
   (    (    R   t   perfectª   s0        	    -c         c   sN   |  Vx= |  d j o/ |  d o d |  d }  n |  d }  |  Vq Wt ‚ d S(   sq  Iterator for the Collatz conjecture's sequence for n.

    It is conjectured that, whatever positive integer n you give to this
    function, the resulting sequence shall ultimately terminate (by yielding 1).
    It is known (by experiment) that the conjecture is good up to n = 10 * 2**58
    (and, hence, also good for any n which is just a power of 2 times some
    positive integer less than this limit).

    The function iterated is, with Z+ = {positive integers}, the union of (: n
    &larr; 2.n :Z+) and (: 6.j+4 &larr;2.j+1 :Z+).  The conjecture effectively
    says that its transitive closure subsumes ({1}:|Z+).
i   i   i   N(   R   t   StopIteration(   R   (    (    R   t   CollatzÊ   s       
	c         C   s¼   |  d j  o t d |  ƒ ‚ n |  d } } x  | o | d L} | d 7} q0 Wd d | >} xW | oO | d L} | | >| } | d 8} | |  j o | d | >O} |  | 8}  qa qa W| S(   s<   Returns the highest natural whose square does not exceed vali    s!   Negative value has no square rooti   i   N(   t   valR   t   vt   bitt   bbt   up(   R&   R)   R*   R'   R(   (    (    R   t   sqrtß   s$      
 

t   Naturalsc           B   sN   t  Z d e f d „  ƒ  YZ e i e _ e ƒ  d „ Z [ e i Z	 d „  Z RS(   Nt   Succ           B   s   t  Z d  d „ Z d „  Z RS(   Nc         C   s6   |  i ƒ  | d  j	 o |  i | ƒ |  |  | <n d  S(   N(   t   selft   cleart   bokt   Nonet   update(   R.   R0   (    (    R   t   __init__÷   s    
c         C   s   |  i |  ƒ S(   N(   R.   t	   __class__(   R.   (    (    R   t   sucý   s    (   t   __name__t
   __module__R1   R3   R5   (    (    (    R   R-   ö   s   c         C   s   | g |  (d  S(   N(   t   zeroR.   (   R.   R8   (    (    R   R3      s    c         C   s?   x/ | t |  ƒ j o |  i |  d i ƒ  ƒ q W|  i | ƒ S(   Niÿÿÿÿ(   t   indt   lenR.   R   R5   t   _Naturals__upget(   R.   R9   (    (    R   t   __getitem__  s      (
   R6   R7   t   dictR-   t   __len__t   __hash__R3   t   listR<   R;   (    (    (    R   R,   õ   s   		N(
   i   i   i   i   i   i   i   i   i   i   (   t   __doc__R   R   R   R   t   theoremR   R   R#   R%   R+   R@   R,   t   naturals(   R#   R   R   R   R   RC   R+   R,   R   RB   R   R%   (    (    R   t   ?	   s   	-		$			 			