| 1 | """ |
| 2 | Test file for Chapter Number Theory. |
| 3 | """ |
| 4 | |
| 5 | r""" |
| 6 | sage: a=IntegerModRing(15)(3); b=IntegerModRing(17)(3); print a, b |
| 7 | 3 3 |
| 8 | sage: a == b |
| 9 | False |
| 10 | sage: R=a.base_ring(); R |
| 11 | Ring of integers modulo 15 |
| 12 | sage: R.characteristic() |
| 13 | 15 |
| 14 | sage: print a+a, a-17, a*a+1, a^3 |
| 15 | 6 1 10 12 |
| 16 | sage: 1/(a+1) |
| 17 | 4 |
| 18 | sage: 1/a |
| 19 | Traceback (most recent call last): |
| 20 | ... |
| 21 | ZeroDivisionError: Inverse does not exist. |
| 22 | sage: z=lift(a); y=ZZ(a); print y, type(y), y==z |
| 23 | 3 <type 'sage.rings.integer.Integer'> True |
| 24 | sage: [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] |
| 26 | sage: [[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]] |
| 28 | sage: p=10^20+39; mod(2,p).multiplicative_order() |
| 29 | 50000000000000000019 |
| 30 | sage: mod(3,p).multiplicative_order() |
| 31 | 100000000000000000038 |
| 32 | sage: n=3^100000; a=n-1; e=100 |
| 33 | sage: timeit('(a^e) % n') # random |
| 34 | 5 loops, best of 3: 387 ms per loop |
| 35 | sage: timeit('power_mod(a,e,n)') # random |
| 36 | 125 loops, best of 3: 3.46 ms per loop |
| 37 | sage: 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] |
| 39 | sage: R = GF(9,name='x'); R |
| 40 | Finite Field in x of size 3^2 |
| 41 | sage: R.polynomial() |
| 42 | x^2 + 2*x + 2 |
| 43 | sage: [r for r in R] |
| 44 | [0, 2*x, x + 1, x + 2, 2, x, 2*x + 2, 2*x + 1, 1] |
| 45 | sage: Q.<x> = PolynomialRing(GF(3)) |
| 46 | sage: R2 = GF(9,name='x',modulus=x^2+1); R2 |
| 47 | Finite Field in x of size 3^2 |
| 48 | sage: p = R(x+1); R2(p) |
| 49 | Traceback (most recent call last): |
| 50 | ... |
| 51 | TypeError: unable to coerce from a finite field other than the prime subfield |
| 52 | sage: rational_reconstruction(411,1000) |
| 53 | -13/17 |
| 54 | sage: rational_reconstruction(409,1000) |
| 55 | Traceback (most recent call last): |
| 56 | ... |
| 57 | ValueError: Rational reconstruction of 409 (mod 1000) does not exist. |
| 58 | sage: def harmonic(n): |
| 59 | ... return sum([1/x for x in range(1,n+1)]) |
| 60 | sage: def harmonic_mod(n,m): |
| 61 | ... return add([1/x % m for x in range(1,n+1)]) |
| 62 | sage: 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) |
| 69 | sage: harmonic(100) == harmonic2(100) |
| 70 | True |
| 71 | sage: a=2; b=3; m=5; n=7; lambda0=(b-a)/m % n; a+lambda0*m |
| 72 | 17 |
| 73 | sage: crt(2,3,5,7) |
| 74 | 17 |
| 75 | sage: 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) |
| 86 | sage: harmonic(100) == harmonic3(100) |
| 87 | True |
| 88 | sage: crt(15,1,30,4) |
| 89 | 45 |
| 90 | sage: crt(15,2,30,4) |
| 91 | Traceback (most recent call last): |
| 92 | ... |
| 93 | ValueError: No solution to crt problem since gcd(30,4) does not divide 15-2 |
| 94 | sage: p=previous_prime(2^400) |
| 95 | sage: timeit('is_pseudoprime(p)') # random |
| 96 | 625 loops, best of 3: 1.07 ms per loop |
| 97 | sage: timeit('is_prime(p)') # random |
| 98 | 5 loops, best of 3: 485 ms per loop |
| 99 | sage: [560 % (x-1) for x in [3,11,17]] |
| 100 | [0, 0, 0] |
| 101 | sage: def count_primes1(n): |
| 102 | ... return add([1 for p in range(n+1) if is_prime(p)]) |
| 103 | sage: def count_primes2(n): |
| 104 | ... return add([1 for p in range(n+1) if is_pseudoprime(p)]) |
| 105 | sage: def count_primes3(n): |
| 106 | ... s=0; p=2 |
| 107 | ... while p <= n: s+=1; p=next_prime(p) |
| 108 | ... return s |
| 109 | sage: def count_primes4(n): |
| 110 | ... s=0; p=2 |
| 111 | ... while p <= n: s+=1; p=next_probable_prime(p) |
| 112 | ... return s |
| 113 | sage: def count_primes5(n): |
| 114 | ... s=0 |
| 115 | ... for p in prime_range(n): s+=1 |
| 116 | ... return s |
| 117 | sage: timeit('count_primes1(10^5)') # random |
| 118 | 5 loops, best of 3: 674 ms per loop |
| 119 | sage: timeit('count_primes2(10^5)') # random |
| 120 | 5 loops, best of 3: 256 ms per loop |
| 121 | sage: timeit('count_primes3(10^5)') # random |
| 122 | 5 loops, best of 3: 49.2 ms per loop |
| 123 | sage: timeit('count_primes4(10^5)') # random |
| 124 | 5 loops, best of 3: 48.6 ms per loop |
| 125 | sage: timeit('count_primes5(10^5)') # random |
| 126 | 125 loops, best of 3: 2.67 ms per loop |
| 127 | sage: p=(2^42737+1)//3; a=3^42737 |
| 128 | sage: timeit('a.gcd(p)') # random |
| 129 | 125 loops, best of 3: 4.3 ms per loop |
| 130 | sage: timeit('a.jacobi(p)') # random |
| 131 | 25 loops, best of 3: 26.1 ms per loop |
| 132 | sage: p=10^10+19; a=mod(17,p); a.log(2) |
| 133 | 6954104378 |
| 134 | sage: mod(2,p)^6954104378 |
| 135 | 17 |
| 136 | sage: p=10^20+39; a=mod(17,p) |
| 137 | sage: time r=a.log(3) # not tested |
| 138 | CPU times: user 89.63 s, sys: 1.70 s, total: 91.33 s |
| 139 | """ |