mò
ógÈFc
@s€dZd„Zd„Zd„Zd„ZdZdZd„Zd„Zd„Z d„Z
defd„ƒYZeƒZ
[dS(sCSimple 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
CsŒd}gd}}|||||}} }yÐxÉ|oÁt || ƒ\}}d|jo"|jo| jo
djnpt‚|o|i|| |fƒn||jo|| | jo
| |jpt‚||| | }} }q5WWn)t
j
otd|||f‚nXxV|oN|iƒ\}} }t ||| || ƒ\}}|djpt‚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). is%d / %d mod %dN(t__algorithmtstacktqtnumtbasetdentntdtbtdivmodtrtAssertionErrortappendtZeroDivisionErrort
ValueErrortpop(
RRRRRRRR
RR((t)/home/eddy/.sys/py/study/maths/natural.pyt dividemod
s*
=2!!cCso|djo|}n|djo|}n|djo|Snx#|djo|||}}qHW|S(sPair-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).
iN(Rta(RR((Rtgcd:s
cGs*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.
iN(tthistotherstotherR(RRR((RthcfLscGs]d}x:|D]2}|p|Snt||ƒ}|||}q
W|djo |Sn|S(sThe 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.
iiN(RRRRtc(RRRR((Rtlcmps
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. iiiiii
iiiicCs€x5tD]-}t||ƒ\}}|djoPqqWd}x;|djo-||djo||7}n|d8}qAW|S(sgReturns the sum of the proper factors of N.
Thus N is perfect precisely if N == factorsum(N).
iiN(t
_early_primestpR tNtiR
tS(RRRRR
((Rt factorsum—s
ccs.d}x!tox5tD]-}t||ƒ\}}|djoPqqWd}xH|djo:||jo-||djo||7}n|d8}qQW||jou|Vd}x|d|>@p|d7}q³W||?dd|>djo)dGt |ƒG|Gt ||d?ƒGHqn|d7}q WdS(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.
iilsUnusually perfectN(
RtTrueRRR tjR
RRthex(RR!RRRR
((Rtperfectªs0
-ccsN|Vx=|djo/|dod|d}n|d}|VqWt‚dS(sqIterator 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
← 2.n :Z+) and (: 6.j+4 ←2.j+1 :Z+). The conjecture effectively
says that its transitive closure subsumes ({1}:|Z+).
iiiN(Rt
StopIteration(R((RtCollatzÊs
cCs¼|djotd|ƒ‚n|d}}x |o|dL}|d7}q0Wdd|>}xW|oO|dL}||>|}|d8}||jo|d|>O}||8}qaqaW|S(s<Returns the highest natural whose square does not exceed valis!Negative value has no square rootiiN(tvalRtvtbittbbtup(R&R)R*R'R(((Rtsqrtßs$
tNaturalscBsNtZdefd„ƒYZeie_eƒd„Z[eiZ d„ZRS(NtSuccBstZdd„Zd„ZRS(NcCs6|iƒ|dj o|i|ƒ|||