Ticket #9395: trac_9395.patch

File trac_9395.patch, 5.3 KB (added by zimmerma, 8 years ago)
  • new file sage/tests/french_book/README

    # HG changeset patch
    # User Paul Zimmermann <zimmerma@loria.fr>
    # Date 1283357207 -7200
    # Node ID bf5776e20e9b58c066656ad417ce183916efeb07
    # Parent  8dec8b43ccca5f104b1e280cb33c8f4c2c1b8f85
    patch for #9395
    
    diff -r 8dec8b43ccca -r bf5776e20e9b sage/tests/french_book/README
    - +  
     1This directory contains all the example code from the french book
     2about Sage by Alexandre Casamayou, Guillaume Connan, Thierry Dumont,
     3Laurent Fousse, François Maltey, Matthias Meulien, Marc Mezzarobba,
     4Clément Pernet, Nicolas Thiéry and Paul Zimmermann. Each file corresponds
     5to a chapter of the book.
     6
     7The book is freely available from http://sagebook.gforge.inria.fr/
  • new file sage/tests/french_book/numbertheory.py

    diff -r 8dec8b43ccca -r bf5776e20e9b sage/tests/french_book/numbertheory.py
    - +  
     1"""
     2Test file for Chapter Number Theory.
     3"""
     4
     5r"""
     6sage: a=IntegerModRing(15)(3); b=IntegerModRing(17)(3); print a, b
     73 3
     8sage: a == b
     9False
     10sage: R=a.base_ring(); R
     11Ring of integers modulo 15
     12sage: R.characteristic()
     1315
     14sage: print a+a, a-17, a*a+1, a^3
     156 1 10 12
     16sage: 1/(a+1)
     174
     18sage: 1/a
     19Traceback (most recent call last):
     20...
     21ZeroDivisionError: Inverse does not exist.
     22sage: z=lift(a); y=ZZ(a); print y, type(y), y==z
     233 <type 'sage.rings.integer.Integer'> True
     24sage: [Mod(x,15).additive_order() for x in range(0,15)]
     25[1, 15, 15, 5, 15, 3, 5, 15, 15, 5, 3, 15, 5, 15, 15]
     26sage: [[x,Mod(x,15).multiplicative_order()] for x in range(1,15) if gcd(x,15)==1]
     27[[1, 1], [2, 4], [4, 2], [7, 4], [8, 4], [11, 2], [13, 4], [14, 2]]
     28sage: p=10^20+39; mod(2,p).multiplicative_order()
     2950000000000000000019
     30sage: mod(3,p).multiplicative_order()
     31100000000000000000038
     32sage: n=3^100000; a=n-1; e=100
     33sage: timeit('(a^e) % n') # random
     345 loops, best of 3: 387 ms per loop
     35sage: timeit('power_mod(a,e,n)') # random
     36125 loops, best of 3: 3.46 ms per loop
     37sage: R = GF(17); [1/R(x) for x in range(1,17)]
     38[1, 9, 6, 13, 7, 3, 5, 15, 2, 12, 14, 10, 4, 11, 8, 16]
     39sage: R = GF(9,name='x'); R
     40Finite Field in x of size 3^2
     41sage: R.polynomial()
     42x^2 + 2*x + 2
     43sage: [r for r in R]
     44[0, 2*x, x + 1, x + 2, 2, x, 2*x + 2, 2*x + 1, 1]
     45sage: Q.<x> = PolynomialRing(GF(3))
     46sage: R2 = GF(9,name='x',modulus=x^2+1); R2
     47Finite Field in x of size 3^2
     48sage: p = R(x+1); R2(p)
     49Traceback (most recent call last):
     50...
     51TypeError: unable to coerce from a finite field other than the prime subfield
     52sage: rational_reconstruction(411,1000)
     53-13/17
     54sage: rational_reconstruction(409,1000)
     55Traceback (most recent call last):
     56...
     57ValueError: Rational reconstruction of 409 (mod 1000) does not exist.
     58sage: def harmonic(n):
     59...    return sum([1/x for x in range(1,n+1)])
     60sage: def harmonic_mod(n,m):
     61...    return add([1/x % m for x in range(1,n+1)])
     62sage: def harmonic2(n):
     63...    q = lcm(range(1,n+1))
     64...    pmax = RR(q*(log(n)+1))
     65...    m = ZZ(2*pmax^2)
     66...    m = ceil(m/q)*q + 1
     67...    a = harmonic_mod(n,m)
     68...    return rational_reconstruction(a,m)
     69sage: harmonic(100) == harmonic2(100)
     70True
     71sage: a=2; b=3; m=5; n=7; lambda0=(b-a)/m % n; a+lambda0*m
     7217
     73sage: crt(2,3,5,7)
     7417
     75sage: def harmonic3(n):
     76...    q = lcm(range(1,n+1))
     77...    pmax = RR(q*(log(n)+1))
     78...    B = ZZ(2*pmax^2)
     79...    m = 1; a = 0; p = 2^63
     80...    while m<B:
     81...        p = next_prime(p)
     82...        b = harmonic_mod(n,p)
     83...        a = crt(a,b,m,p)
     84...        m = m*p
     85...    return rational_reconstruction(a,m)
     86sage: harmonic(100) == harmonic3(100)
     87True
     88sage: crt(15,1,30,4)
     8945
     90sage: crt(15,2,30,4)
     91Traceback (most recent call last):
     92...
     93ValueError: No solution to crt problem since gcd(30,4) does not divide 15-2
     94sage: p=previous_prime(2^400)
     95sage: timeit('is_pseudoprime(p)') # random
     96625 loops, best of 3: 1.07 ms per loop
     97sage: timeit('is_prime(p)') # random
     985 loops, best of 3: 485 ms per loop
     99sage: [560 % (x-1) for x in [3,11,17]]
     100[0, 0, 0]
     101sage: def count_primes1(n):
     102...    return add([1 for p in range(n+1) if is_prime(p)])
     103sage: def count_primes2(n):
     104...    return add([1 for p in range(n+1) if is_pseudoprime(p)])
     105sage: def count_primes3(n):
     106...    s=0; p=2
     107...    while p <= n: s+=1; p=next_prime(p)
     108...    return s
     109sage: def count_primes4(n):
     110...    s=0; p=2
     111...    while p <= n: s+=1; p=next_probable_prime(p)
     112...    return s
     113sage: def count_primes5(n):
     114...    s=0
     115...    for p in prime_range(n): s+=1
     116...    return s
     117sage: timeit('count_primes1(10^5)') # random
     1185 loops, best of 3: 674 ms per loop
     119sage: timeit('count_primes2(10^5)') # random
     1205 loops, best of 3: 256 ms per loop
     121sage: timeit('count_primes3(10^5)') # random
     1225 loops, best of 3: 49.2 ms per loop
     123sage: timeit('count_primes4(10^5)') # random
     1245 loops, best of 3: 48.6 ms per loop
     125sage: timeit('count_primes5(10^5)') # random
     126125 loops, best of 3: 2.67 ms per loop
     127sage: p=(2^42737+1)//3; a=3^42737
     128sage: timeit('a.gcd(p)') # random
     129125 loops, best of 3: 4.3 ms per loop
     130sage: timeit('a.jacobi(p)') # random
     13125 loops, best of 3: 26.1 ms per loop
     132sage: p=10^10+19; a=mod(17,p); a.log(2)
     1336954104378
     134sage: mod(2,p)^6954104378
     13517
     136sage: p=10^20+39; a=mod(17,p)
     137sage: time r=a.log(3) # not tested
     138CPU times: user 89.63 s, sys: 1.70 s, total: 91.33 s
     139"""