passagemath-repl 10.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. passagemath_repl-10.5.1.data/scripts/sage-cachegrind +25 -0
  2. passagemath_repl-10.5.1.data/scripts/sage-callgrind +16 -0
  3. passagemath_repl-10.5.1.data/scripts/sage-cleaner +230 -0
  4. passagemath_repl-10.5.1.data/scripts/sage-coverage +327 -0
  5. passagemath_repl-10.5.1.data/scripts/sage-eval +14 -0
  6. passagemath_repl-10.5.1.data/scripts/sage-fixdoctests +710 -0
  7. passagemath_repl-10.5.1.data/scripts/sage-inline-fortran +12 -0
  8. passagemath_repl-10.5.1.data/scripts/sage-ipynb2rst +50 -0
  9. passagemath_repl-10.5.1.data/scripts/sage-ipython +16 -0
  10. passagemath_repl-10.5.1.data/scripts/sage-massif +25 -0
  11. passagemath_repl-10.5.1.data/scripts/sage-notebook +267 -0
  12. passagemath_repl-10.5.1.data/scripts/sage-omega +25 -0
  13. passagemath_repl-10.5.1.data/scripts/sage-preparse +302 -0
  14. passagemath_repl-10.5.1.data/scripts/sage-run +27 -0
  15. passagemath_repl-10.5.1.data/scripts/sage-run-cython +10 -0
  16. passagemath_repl-10.5.1.data/scripts/sage-runtests +9 -0
  17. passagemath_repl-10.5.1.data/scripts/sage-startuptime.py +163 -0
  18. passagemath_repl-10.5.1.data/scripts/sage-valgrind +34 -0
  19. passagemath_repl-10.5.1.dist-info/METADATA +77 -0
  20. passagemath_repl-10.5.1.dist-info/RECORD +162 -0
  21. passagemath_repl-10.5.1.dist-info/WHEEL +5 -0
  22. passagemath_repl-10.5.1.dist-info/top_level.txt +1 -0
  23. sage/all__sagemath_repl.py +119 -0
  24. sage/doctest/__init__.py +4 -0
  25. sage/doctest/__main__.py +236 -0
  26. sage/doctest/all.py +4 -0
  27. sage/doctest/check_tolerance.py +261 -0
  28. sage/doctest/control.py +1727 -0
  29. sage/doctest/external.py +534 -0
  30. sage/doctest/fixtures.py +383 -0
  31. sage/doctest/forker.py +2665 -0
  32. sage/doctest/marked_output.py +102 -0
  33. sage/doctest/parsing.py +1708 -0
  34. sage/doctest/parsing_test.py +79 -0
  35. sage/doctest/reporting.py +733 -0
  36. sage/doctest/rif_tol.py +124 -0
  37. sage/doctest/sources.py +1657 -0
  38. sage/doctest/test.py +584 -0
  39. sage/doctest/tests/1second.rst +4 -0
  40. sage/doctest/tests/99seconds.rst +4 -0
  41. sage/doctest/tests/abort.rst +5 -0
  42. sage/doctest/tests/atexit.rst +7 -0
  43. sage/doctest/tests/fail_and_die.rst +6 -0
  44. sage/doctest/tests/initial.rst +15 -0
  45. sage/doctest/tests/interrupt.rst +7 -0
  46. sage/doctest/tests/interrupt_diehard.rst +14 -0
  47. sage/doctest/tests/keyboardinterrupt.rst +11 -0
  48. sage/doctest/tests/longtime.rst +5 -0
  49. sage/doctest/tests/nodoctest +5 -0
  50. sage/doctest/tests/random_seed.rst +4 -0
  51. sage/doctest/tests/show_skipped.rst +18 -0
  52. sage/doctest/tests/sig_on.rst +9 -0
  53. sage/doctest/tests/simple_failure.rst +8 -0
  54. sage/doctest/tests/sleep_and_raise.rst +106 -0
  55. sage/doctest/tests/tolerance.rst +31 -0
  56. sage/doctest/util.py +750 -0
  57. sage/interfaces/cleaner.py +48 -0
  58. sage/interfaces/quit.py +163 -0
  59. sage/misc/all__sagemath_repl.py +51 -0
  60. sage/misc/banner.py +235 -0
  61. sage/misc/benchmark.py +221 -0
  62. sage/misc/classgraph.py +134 -0
  63. sage/misc/copying.py +22 -0
  64. sage/misc/cython.py +694 -0
  65. sage/misc/dev_tools.py +745 -0
  66. sage/misc/edit_module.py +304 -0
  67. sage/misc/explain_pickle.py +3079 -0
  68. sage/misc/gperftools.py +361 -0
  69. sage/misc/inline_fortran.py +212 -0
  70. sage/misc/messaging.py +86 -0
  71. sage/misc/pager.py +21 -0
  72. sage/misc/profiler.py +179 -0
  73. sage/misc/python.py +70 -0
  74. sage/misc/remote_file.py +53 -0
  75. sage/misc/sage_eval.py +249 -0
  76. sage/misc/sage_input.py +3621 -0
  77. sage/misc/sagedoc.py +1742 -0
  78. sage/misc/sh.py +38 -0
  79. sage/misc/trace.py +90 -0
  80. sage/repl/__init__.py +16 -0
  81. sage/repl/all.py +15 -0
  82. sage/repl/attach.py +625 -0
  83. sage/repl/configuration.py +186 -0
  84. sage/repl/display/__init__.py +1 -0
  85. sage/repl/display/fancy_repr.py +354 -0
  86. sage/repl/display/formatter.py +318 -0
  87. sage/repl/display/jsmol_iframe.py +290 -0
  88. sage/repl/display/pretty_print.py +153 -0
  89. sage/repl/display/util.py +163 -0
  90. sage/repl/image.py +302 -0
  91. sage/repl/inputhook.py +91 -0
  92. sage/repl/interface_magic.py +298 -0
  93. sage/repl/interpreter.py +854 -0
  94. sage/repl/ipython_extension.py +593 -0
  95. sage/repl/ipython_kernel/__init__.py +1 -0
  96. sage/repl/ipython_kernel/__main__.py +4 -0
  97. sage/repl/ipython_kernel/all_jupyter.py +10 -0
  98. sage/repl/ipython_kernel/install.py +301 -0
  99. sage/repl/ipython_kernel/interact.py +278 -0
  100. sage/repl/ipython_kernel/kernel.py +217 -0
  101. sage/repl/ipython_kernel/widgets.py +466 -0
  102. sage/repl/ipython_kernel/widgets_sagenb.py +587 -0
  103. sage/repl/ipython_tests.py +163 -0
  104. sage/repl/load.py +326 -0
  105. sage/repl/preparse.py +2218 -0
  106. sage/repl/prompts.py +90 -0
  107. sage/repl/rich_output/__init__.py +4 -0
  108. sage/repl/rich_output/backend_base.py +648 -0
  109. sage/repl/rich_output/backend_doctest.py +316 -0
  110. sage/repl/rich_output/backend_emacs.py +151 -0
  111. sage/repl/rich_output/backend_ipython.py +596 -0
  112. sage/repl/rich_output/buffer.py +311 -0
  113. sage/repl/rich_output/display_manager.py +829 -0
  114. sage/repl/rich_output/example.avi +0 -0
  115. sage/repl/rich_output/example.canvas3d +1 -0
  116. sage/repl/rich_output/example.dvi +0 -0
  117. sage/repl/rich_output/example.flv +0 -0
  118. sage/repl/rich_output/example.gif +0 -0
  119. sage/repl/rich_output/example.jpg +0 -0
  120. sage/repl/rich_output/example.mkv +0 -0
  121. sage/repl/rich_output/example.mov +0 -0
  122. sage/repl/rich_output/example.mp4 +0 -0
  123. sage/repl/rich_output/example.ogv +0 -0
  124. sage/repl/rich_output/example.pdf +0 -0
  125. sage/repl/rich_output/example.png +0 -0
  126. sage/repl/rich_output/example.svg +54 -0
  127. sage/repl/rich_output/example.webm +0 -0
  128. sage/repl/rich_output/example.wmv +0 -0
  129. sage/repl/rich_output/example_jmol.spt.zip +0 -0
  130. sage/repl/rich_output/example_wavefront_scene.mtl +7 -0
  131. sage/repl/rich_output/example_wavefront_scene.obj +17 -0
  132. sage/repl/rich_output/output_basic.py +391 -0
  133. sage/repl/rich_output/output_browser.py +103 -0
  134. sage/repl/rich_output/output_catalog.py +54 -0
  135. sage/repl/rich_output/output_graphics.py +320 -0
  136. sage/repl/rich_output/output_graphics3d.py +345 -0
  137. sage/repl/rich_output/output_video.py +231 -0
  138. sage/repl/rich_output/preferences.py +432 -0
  139. sage/repl/rich_output/pretty_print.py +339 -0
  140. sage/repl/rich_output/test_backend.py +201 -0
  141. sage/repl/user_globals.py +214 -0
  142. sage/tests/all.py +0 -0
  143. sage/tests/all__sagemath_repl.py +3 -0
  144. sage/tests/article_heuberger_krenn_kropf_fsm-in-sage.py +630 -0
  145. sage/tests/arxiv_0812_2725.py +351 -0
  146. sage/tests/benchmark.py +1925 -0
  147. sage/tests/book_schilling_zabrocki_kschur_primer.py +795 -0
  148. sage/tests/book_stein_ent.py +651 -0
  149. sage/tests/book_stein_modform.py +558 -0
  150. sage/tests/cmdline.py +796 -0
  151. sage/tests/combinatorial_hopf_algebras.py +52 -0
  152. sage/tests/finite_poset.py +623 -0
  153. sage/tests/functools_partial_src.py +27 -0
  154. sage/tests/gosper-sum.py +218 -0
  155. sage/tests/lazy_imports.py +28 -0
  156. sage/tests/modular_group_cohomology.py +80 -0
  157. sage/tests/numpy.py +21 -0
  158. sage/tests/parigp.py +76 -0
  159. sage/tests/startup.py +27 -0
  160. sage/tests/symbolic-series.py +76 -0
  161. sage/tests/sympy.py +16 -0
  162. sage/tests/test_deprecation.py +31 -0
@@ -0,0 +1,651 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ # sage.doctest: needs sage.libs.pari
3
+ """
4
+ This file contains all the example code from the published book
5
+ 'Elementary Number Theory: Primes, Congruences, and Secrets' by
6
+ William Stein, Springer-Verlag, 2009.
7
+ """
8
+
9
+ """
10
+ sage: prime_range(10,50)
11
+ [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
12
+ sage: [n for n in range(10,30) if not is_prime(n)]
13
+ [10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28]
14
+ sage: gcd(97,100)
15
+ 1
16
+ sage: gcd(97 * 10^15, 19^20 * 97^2)
17
+ 97
18
+ sage: factor(1275)
19
+ 3 * 5^2 * 17
20
+ sage: factor(2007)
21
+ 3^2 * 223
22
+ sage: factor(31415926535898)
23
+ 2 * 3 * 53 * 73 * 2531 * 534697
24
+ sage: n = 7403756347956171282804679609742957314259318888\
25
+ ....: 9231289084936232638972765034028266276891996419625117\
26
+ ....: 8439958943305021275853701189680982867331732731089309\
27
+ ....: 0055250511687706329907239638078671008609696253793465\
28
+ ....: 0563796359
29
+ sage: len(n.str(2))
30
+ 704
31
+ sage: len(n.str(10))
32
+ 212
33
+ sage: n.is_prime() # this is instant
34
+ False
35
+ sage: p = 2^32582657 - 1
36
+ sage: p.ndigits() # needs sage.rings.real_interval_field
37
+ 9808358
38
+ sage: s = p.str(10) # this takes a long time
39
+ sage: len(s) # s is a very long string (long time)
40
+ 9808358
41
+ sage: s[:20] # the first 20 digits of p (long time)
42
+ '12457502601536945540'
43
+ sage: s[-20:] # the last 20 digits (long time)
44
+ '11752880154053967871'
45
+
46
+ sage: # needs sage.symbolic
47
+ sage: prime_pi(6)
48
+ 3
49
+ sage: prime_pi(100)
50
+ 25
51
+ sage: prime_pi(3000000)
52
+ 216816
53
+
54
+ sage: # needs sage.plot sage.symbolic
55
+ sage: plot(prime_pi, 1,1000, rgbcolor=(0,0,1))
56
+ Graphics object consisting of 1 graphics primitive
57
+ sage: P = plot(Li, 2,10000, rgbcolor='purple')
58
+ sage: Q = plot(prime_pi, 2,10000, rgbcolor='black')
59
+ sage: R = plot(sqrt(x)*log(x), 2,10000, rgbcolor='red')
60
+ sage: show(P + Q + R, xmin=0, figsize=[8,3])
61
+
62
+ sage: R = Integers(3)
63
+ sage: list(R)
64
+ [0, 1, 2]
65
+ sage: R = Integers(10)
66
+ sage: a = R(3) # create an element of Z/10Z
67
+ sage: a.multiplicative_order()
68
+ 4
69
+ sage: [a^i for i in range(15)]
70
+ [1, 3, 9, 7, 1, 3, 9, 7, 1, 3, 9, 7, 1, 3, 9]
71
+ sage: euler_phi(2007)
72
+ 1332
73
+ sage: n = 20
74
+ sage: k = euler_phi(n); k
75
+ 8
76
+ sage: [Mod(x,n)^k for x in range(n) if gcd(x,n) == 1]
77
+ [1, 1, 1, 1, 1, 1, 1, 1]
78
+ sage: for n in range(1,10):
79
+ ....: print("{} {} {}".format(n, factorial(n-1) % n, -1 % n))
80
+ 1 0 0
81
+ 2 1 1
82
+ 3 2 2
83
+ 4 2 3
84
+ 5 4 4
85
+ 6 0 5
86
+ 7 6 6
87
+ 8 0 7
88
+ 9 0 8
89
+ sage: CRT(2,3, 3, 5)
90
+ 8
91
+ sage: CRT_list([2,3,2], [3,5,7])
92
+ 23
93
+ sage: xgcd(5,7)
94
+ (1, 3, -2)
95
+ sage: xgcd(130,61)
96
+ (1, 23, -49)
97
+ sage: a = Mod(17, 61)
98
+ sage: a^(-1)
99
+ 18
100
+ sage: 100.str(2)
101
+ '1100100'
102
+ sage: 0*2^0 + 0*2^1 + 1*2^2 + 0*2^3 + 0*2^4 + 1*2^5 + 1*2^6
103
+ 100
104
+ sage: Mod(7,100)^91
105
+ 43
106
+ sage: 7^91
107
+ 80153343160247310515380886994816022539378033762994852007501964604841680190743
108
+ sage: n = 95468093486093450983409583409850934850938459083
109
+ sage: Mod(2,n)^(n-1)
110
+ 34173444139265553870830266378598407069248687241
111
+ sage: factor(n) # takes up to a few seconds.
112
+ 1610302526747 * 59285812386415488446397191791023889
113
+ sage: n = 95468093486093450983409583409850934850938459083
114
+ sage: is_prime(n)
115
+ False
116
+ sage: for p in primes(100):
117
+ ....: if is_prime(2^p - 1):
118
+ ....: print("{} {}".format(p, 2^p - 1))
119
+ 2 3
120
+ 3 7
121
+ 5 31
122
+ 7 127
123
+ 13 8191
124
+ 17 131071
125
+ 19 524287
126
+ 31 2147483647
127
+ 61 2305843009213693951
128
+ 89 618970019642690137449562111
129
+ sage: def is_prime_lucas_lehmer(p):
130
+ ....: s = Mod(4, 2^p - 1)
131
+ ....: for i in range(3, p+1):
132
+ ....: s = s^2 - 2
133
+ ....: return s == 0
134
+ sage: # Check primality of 2^9941 - 1
135
+ sage: is_prime_lucas_lehmer(9941)
136
+ True
137
+ sage: # Check primality of 2^next_prime(1000)-1
138
+ sage: is_prime_lucas_lehmer(next_prime(1000))
139
+ False
140
+ sage: for p in primes(20):
141
+ ....: print("{} {}".format(p, primitive_root(p)))
142
+ 2 1
143
+ 3 2
144
+ 5 2
145
+ 7 3
146
+ 11 2
147
+ 13 2
148
+ 17 3
149
+ 19 2
150
+ sage: R.<x> = PolynomialRing(Integers(13))
151
+ sage: f = x^15 + 1
152
+ sage: f.roots()
153
+ [(12, 1), (10, 1), (4, 1)]
154
+ sage: f(12)
155
+ 0
156
+ sage: R.<x> = PolynomialRing(Integers(13))
157
+ sage: f = x^6 + 1
158
+ sage: f.roots()
159
+ [(11, 1), (8, 1), (7, 1), (6, 1), (5, 1), (2, 1)]
160
+ sage: log(19683.0)
161
+ 9.88751059801299
162
+ sage: log(3.0)
163
+ 1.09861228866811
164
+ sage: log(19683.0) / log(3.0)
165
+ 9.00000000000000
166
+ sage: plot(log, 0.1, 10, rgbcolor=(0,0,1)) # needs sage.plot
167
+ Graphics object consisting of 1 graphics primitive
168
+ sage: p = 53
169
+ sage: R = Integers(p)
170
+ sage: a = R.multiplicative_generator()
171
+ sage: v = sorted([(a^n, n) for n in range(p-1)])
172
+ sage: G = plot(point(v,pointsize=50,rgbcolor=(0,0,1))) # needs sage.plot
173
+ sage: H = plot(line(v,rgbcolor=(0.5,0.5,0.5))) # needs sage.plot
174
+ sage: G + H # needs sage.plot
175
+ Graphics object consisting of 2 graphics primitives
176
+ sage: q = 93450983094850938450983409623
177
+ sage: q.is_prime()
178
+ True
179
+ sage: is_prime((q-1)//2)
180
+ True
181
+ sage: g = Mod(-2, q)
182
+ sage: g.multiplicative_order()
183
+ 93450983094850938450983409622
184
+ sage: n = 18319922375531859171613379181
185
+ sage: m = 82335836243866695680141440300
186
+ sage: g^n
187
+ 45416776270485369791375944998
188
+ sage: g^m
189
+ 15048074151770884271824225393
190
+ sage: (g^n)^m
191
+ 85771409470770521212346739540
192
+ sage: (g^m)^n
193
+ 85771409470770521212346739540
194
+ sage: def rsa(bits):
195
+ ....: # only prove correctness up to 1024 bits
196
+ ....: proof = (bits <= 1024)
197
+ ....: while True:
198
+ ....: p = next_prime(ZZ.random_element(2**(bits//2 +1)),
199
+ ....: proof=proof)
200
+ ....: q = next_prime(ZZ.random_element(2**(bits//2 +1)),
201
+ ....: proof=proof)
202
+ ....: if (p != q and p > 2 and q > 2): break
203
+ ....: n = p * q
204
+ ....: phi_n = (p-1) * (q-1)
205
+ ....: while True:
206
+ ....: e = ZZ.random_element(1,phi_n)
207
+ ....: if gcd(e,phi_n) == 1: break
208
+ ....: d = lift(Mod(e,phi_n)^(-1))
209
+ ....: return e, d, n
210
+ sage: def encrypt(m, e, n):
211
+ ....: return lift(Mod(m,n)^e)
212
+ sage: def decrypt(c, d, n):
213
+ ....: return lift(Mod(c,n)^d)
214
+ sage: e,d,n = rsa(20)
215
+ sage: c = encrypt(123, e, n)
216
+ sage: decrypt(c, d, n)
217
+ 123
218
+ sage: def encode(s):
219
+ ....: s = str(s) # make input a string
220
+ ....: return sum(ord(s[i])*256^i for i in range(len(s)))
221
+ sage: def decode(n):
222
+ ....: n = Integer(n) # make input an integer
223
+ ....: v = []
224
+ ....: while n != 0:
225
+ ....: v.append(chr(n % 256))
226
+ ....: n //= 256 # this replaces n by floor(n/256).
227
+ ....: return ''.join(v)
228
+ sage: m = encode('Run Nikita!'); m
229
+ 40354769014714649421968722
230
+ sage: decode(m)
231
+ 'Run Nikita!'
232
+ sage: def crack_rsa(n, phi_n):
233
+ ....: R.<x> = PolynomialRing(QQ)
234
+ ....: f = x^2 - (n+1 -phi_n)*x + n
235
+ ....: return [b for b, _ in f.roots()]
236
+ sage: crack_rsa(31615577110997599711, 31615577098574867424)
237
+ [8850588049, 3572144239]
238
+ sage: def crack_when_pq_close(n):
239
+ ....: t = Integer(ceil(sqrt(n)))
240
+ ....: while True:
241
+ ....: k = t^2 - n
242
+ ....: if k > 0:
243
+ ....: s = Integer(int(round(sqrt(t^2 - n))))
244
+ ....: if s^2 + n == t^2:
245
+ ....: return t+s, t-s
246
+ ....: t += 1
247
+ sage: crack_when_pq_close(23360947609)
248
+ (153649, 152041)
249
+ sage: p = next_prime(2^128); p
250
+ 340282366920938463463374607431768211507
251
+ sage: q = next_prime(p)
252
+ sage: crack_when_pq_close(p*q)
253
+ (340282366920938463463374607431768211537,
254
+ 340282366920938463463374607431768211507)
255
+ sage: def crack_given_decrypt(n, m):
256
+ ....: n = Integer(n); m = Integer(m); # some type checking
257
+ ....: # Step 1: divide out powers of 2
258
+ ....: while True:
259
+ ....: if is_odd(m): break
260
+ ....: divide_out = True
261
+ ....: for i in range(5):
262
+ ....: a = randrange(1,n)
263
+ ....: if gcd(a,n) == 1:
264
+ ....: if Mod(a,n)^(m//2) != 1:
265
+ ....: divide_out = False
266
+ ....: break
267
+ ....: if divide_out:
268
+ ....: m = m//2
269
+ ....: else:
270
+ ....: break
271
+ ....: # Step 2: Compute GCD
272
+ ....: while True:
273
+ ....: a = randrange(1,n)
274
+ ....: g = gcd(lift(Mod(a, n)^(m//2)) - 1, n)
275
+ ....: if g != 1 and g != n:
276
+ ....: return g
277
+ sage: n=32295194023343; e=29468811804857; d=11127763319273
278
+ sage: p = crack_given_decrypt(n, e*d - 1)
279
+ sage: p in (737531, n/737531) # could be other prime divisor
280
+ True
281
+ sage: factor(n)
282
+ 737531 * 43788253
283
+ sage: e = 22601762315966221465875845336488389513
284
+ sage: d = 31940292321834506197902778067109010093
285
+ sage: n = 268494924039590992469444675130990465673
286
+ sage: p = crack_given_decrypt(n, e*d - 1) # not tested, known bug (see :issue:`32097`)
287
+ sage: p # random output (could be other prime divisor) # not tested
288
+ 13432418150982799907
289
+ sage: n % p # not tested
290
+ 0
291
+ sage: set_random_seed(0)
292
+ sage: p = next_prime(randrange(2^96))
293
+ sage: q = next_prime(randrange(2^97))
294
+ sage: n = p * q
295
+ sage: qsieve(n) # long time (8s on sage.math, 2011)
296
+ [(6340271405786663791648052309, 1), (46102313108592180286398757159, 1)]
297
+ sage: legendre_symbol(2,3)
298
+ -1
299
+ sage: legendre_symbol(1,3)
300
+ 1
301
+ sage: legendre_symbol(3,5)
302
+ -1
303
+ sage: legendre_symbol(Mod(3,5), 5)
304
+ -1
305
+ sage: legendre_symbol(69,389)
306
+ 1
307
+ sage: def kr(a, p):
308
+ ....: if Mod(a,p)^((p-1)//2) == 1:
309
+ ....: return 1
310
+ ....: else:
311
+ ....: return -1
312
+ sage: for a in range(1,5):
313
+ ....: print("{} {}".format(a, kr(a,5)))
314
+ 1 1
315
+ 2 -1
316
+ 3 -1
317
+ 4 1
318
+ sage: p = 726377359
319
+ sage: Mod(3, p)^((p-1)//2)
320
+ 726377358
321
+ sage: def gauss(a, p):
322
+ ....: # make the list of numbers reduced modulo p
323
+ ....: v = [(n*a)%p for n in range(1, (p-1)//2 + 1)]
324
+ ....: # normalize them to be in the range -p/2 to p/2
325
+ ....: v = [(x if (x < p/2) else x - p) for x in v]
326
+ ....: # sort and print the resulting numbers
327
+ ....: v.sort()
328
+ ....: print(v)
329
+ ....: # count the number that are negative
330
+ ....: num_neg = len([x for x in v if x < 0])
331
+ ....: return (-1)^num_neg
332
+ sage: gauss(2, 13)
333
+ [-5, -3, -1, 2, 4, 6]
334
+ -1
335
+ sage: legendre_symbol(2,13)
336
+ -1
337
+ sage: gauss(4, 13)
338
+ [-6, -5, -2, -1, 3, 4]
339
+ 1
340
+ sage: legendre_symbol(4,13)
341
+ 1
342
+ sage: gauss(2,31)
343
+ [-15, -13, -11, -9, -7, -5, -3, -1, 2, 4, 6, 8, 10, 12, 14]
344
+ 1
345
+ sage: legendre_symbol(2,31)
346
+ 1
347
+ sage: K.<zeta> = CyclotomicField(5)
348
+ sage: zeta^5
349
+ 1
350
+ sage: 1/zeta
351
+ -zeta^3 - zeta^2 - zeta - 1
352
+ sage: def gauss_sum(a, p):
353
+ ....: K.<zeta> = CyclotomicField(p)
354
+ ....: return sum(legendre_symbol(n,p) * zeta^(a*n) for n in range(1,p))
355
+ sage: g2 = gauss_sum(2,5); g2
356
+ 2*zeta^3 + 2*zeta^2 + 1
357
+ sage: g2.complex_embedding()
358
+ -2.236067977... + ...e-16*I
359
+ sage: g2^2
360
+ 5
361
+ sage: [gauss_sum(a, 7)^2 for a in range(1,7)]
362
+ [-7, -7, -7, -7, -7, -7]
363
+ sage: [gauss_sum(a, 13)^2 for a in range(1,13)]
364
+ [13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13]
365
+ sage: S.<x> = PolynomialRing(GF(13))
366
+ sage: R.<alpha> = S.quotient(x^2 - 3)
367
+ sage: (2+3*alpha)*(1+2*alpha)
368
+ 7*alpha + 7
369
+ sage: def find_sqrt(a, p):
370
+ ....: assert (p-1)%4 == 0
371
+ ....: assert legendre_symbol(a,p) == 1
372
+ ....: S.<x> = PolynomialRing(GF(p))
373
+ ....: R.<alpha> = S.quotient(x^2 - a)
374
+ ....: while True:
375
+ ....: z = GF(p).random_element()
376
+ ....: w = (1 + z*alpha)^((p-1)//2)
377
+ ....: (u, v) = (w[0], w[1])
378
+ ....: if v != 0: break
379
+ ....: if (-u/v)^2 == a: return -u/v
380
+ ....: if ((1-u)/v)^2 == a: return (1-u)/v
381
+ ....: if ((-1-u)/v)^2 == a: return (-1-u)/v
382
+ sage: b = find_sqrt(3,13)
383
+ sage: b # random: either 9 or 3
384
+ 9
385
+ sage: b^2
386
+ 3
387
+ sage: b = find_sqrt(3,13)
388
+ sage: b # see, it's random
389
+ 4
390
+ sage: find_sqrt(5,389) # random: either 303 or 86
391
+ 303
392
+ sage: find_sqrt(5,389) # see, it's random
393
+ 86
394
+
395
+ # Several of the examples below had to be changed due to improved
396
+ # behavior of the continued_fraction function #8017 and #14567.
397
+
398
+ sage: continued_fraction(17/23)
399
+ [0; 1, 2, 1, 5]
400
+ sage: reset('e')
401
+ sage: continued_fraction(e)
402
+ [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, ...]
403
+ sage: continued_fraction_list(e, bits=21)
404
+ [2, 1, 2, 1, 1, 4, 1, 1, 6]
405
+ sage: continued_fraction_list(e, bits=30)
406
+ [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8]
407
+ sage: a = continued_fraction(17/23); a
408
+ [0; 1, 2, 1, 5]
409
+ sage: a.value()
410
+ 17/23
411
+ sage: b = continued_fraction(6/23); b
412
+ [0; 3, 1, 5]
413
+ sage: c = continued_fraction(pi); c
414
+ [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2, ...]
415
+ sage: [c.convergent(i) for i in range(5)]
416
+ [3, 22/7, 333/106, 355/113, 103993/33102]
417
+ sage: [c.p(n)*c.q(n-1) - c.q(n)*c.p(n-1) for n in range(-1, 13)]
418
+ [1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1]
419
+ sage: [c.p(n)*c.q(n-2) - c.q(n)*c.p(n-2) for n in range(13)]
420
+ [3, -7, 15, -1, 292, -1, 1, -1, 2, -1, 3, -1, 14]
421
+ sage: c = continued_fraction([1,2,3,4,5])
422
+ sage: c.convergents()
423
+ [1, 3/2, 10/7, 43/30, 225/157]
424
+ sage: [c.p(n) for n in range(len(c))]
425
+ [1, 3, 10, 43, 225]
426
+ sage: [c.q(n) for n in range(len(c))]
427
+ [1, 2, 7, 30, 157]
428
+ sage: c = continued_fraction([1,1,1,1,1,1,1,1])
429
+ sage: v = [(i, c.p(i)/c.q(i)) for i in range(len(c))]
430
+
431
+ sage: # needs sage.plot
432
+ sage: P = point(v, rgbcolor=(0,0,1), pointsize=40)
433
+ sage: L = line(v, rgbcolor=(0.5,0.5,0.5))
434
+ sage: L2 = line([(0,c.value()), (len(c)-1,c.value())],
435
+ ....: thickness=0.5, rgbcolor=(0.7,0,0))
436
+ sage: (L + L2 + P).show(xmin=0, ymin=1)
437
+
438
+ sage: def cf(bits):
439
+ ....: x = (1 + sqrt(RealField(bits)(5))) / 2
440
+ ....: return continued_fraction(x)
441
+ sage: cf(10)
442
+ [1; 1, 1, 1, 1, 1, 1, 2]
443
+ sage: cf(30)
444
+ [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]
445
+ sage: cf(50)
446
+ [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]
447
+ sage: def cf_sqrt_d(d, bits):
448
+ ....: x = sqrt(RealField(bits)(d))
449
+ ....: return continued_fraction(x)
450
+ sage: cf_sqrt_d(389,50)
451
+ [19; 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38]
452
+ sage: cf_sqrt_d(389,100)
453
+ [19; 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 2]
454
+ sage: def newton_root(f, iterates=2, x0=0, prec=53):
455
+ ....: x = RealField(prec)(x0)
456
+ ....: R = PolynomialRing(ZZ,'x')
457
+ ....: f = R(f)
458
+ ....: g = f.derivative()
459
+ ....: for i in range(iterates):
460
+ ....: x = x - f(x)/g(x)
461
+ ....: return x
462
+ sage: reset('x')
463
+ sage: a = newton_root(3847*x^2 - 14808904*x + 36527265); a
464
+ 2.46815700480740
465
+ sage: cf = continued_fraction(a); cf
466
+ [2; 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 103, 8, 1, 2, 3, 2]
467
+ sage: c = cf[:12]; c
468
+ [2; 2, 7, 2, 1, 5, 1, 1, 1, 1, 2]
469
+ sage: c.value()
470
+ 9495/3847
471
+ sage: def sum_of_two_squares_naive(n):
472
+ ....: for i in range(int(sqrt(n))):
473
+ ....: if is_square(n - i^2):
474
+ ....: return i, (Integer(n-i^2)).sqrt()
475
+ ....: return "%s is not a sum of two squares"%n
476
+ sage: sum_of_two_squares_naive(23)
477
+ '23 is not a sum of two squares'
478
+ sage: sum_of_two_squares_naive(389)
479
+ (10, 17)
480
+ sage: sum_of_two_squares_naive(2007)
481
+ '2007 is not a sum of two squares'
482
+ sage: sum_of_two_squares_naive(2008)
483
+ '2008 is not a sum of two squares'
484
+ sage: sum_of_two_squares_naive(2009)
485
+ (28, 35)
486
+ sage: 28^2 + 35^2
487
+ 2009
488
+ sage: sum_of_two_squares_naive(2*3^4*5*7^2*13)
489
+ (189, 693)
490
+ sage: def sum_of_two_squares(p):
491
+ ....: p = Integer(p)
492
+ ....: assert p%4 == 1, "p must be 1 modulo 4"
493
+ ....: r = Mod(-1,p).sqrt().lift()
494
+ ....: v = continued_fraction(-r/p)
495
+ ....: n = floor(sqrt(p))
496
+ ....: for x in v.convergents():
497
+ ....: c = r*x.denominator() + p*x.numerator()
498
+ ....: if -n <= c and c <= n:
499
+ ....: return (abs(x.denominator()),abs(c))
500
+ sage: p = next_prime(next_prime(10^10))
501
+ sage: sum_of_two_squares(p)
502
+ (55913, 82908)
503
+ sage: sum_of_two_squares_naive(p)
504
+ (55913, 82908)
505
+ sage: E = EllipticCurve([-5, 4])
506
+ sage: E
507
+ Elliptic Curve defined by y^2 = x^3 - 5*x + 4
508
+ over Rational Field
509
+ sage: P = E.plot(thickness=4,rgbcolor=(0.1,0.7,0.1)) # needs sage.plot
510
+ sage: P.show(figsize=[4,6]) # needs sage.plot
511
+ sage: E = EllipticCurve(GF(37), [1,0])
512
+ sage: E
513
+ Elliptic Curve defined by y^2 = x^3 + x over
514
+ Finite Field of size 37
515
+ sage: E.plot(pointsize=45) # needs sage.plot
516
+ Graphics object consisting of 1 graphics primitive
517
+ sage: E = EllipticCurve([-5,4])
518
+ sage: P = E([1,0]); Q = E([0,2])
519
+ sage: P + Q
520
+ (3 : 4 : 1)
521
+ sage: P + P
522
+ (0 : 1 : 0)
523
+ sage: P + Q + Q + Q + Q
524
+ (350497/351649 : 16920528/208527857 : 1)
525
+ sage: R.<x1,y1,x2,y2,x3,y3,a,b> = QQ[]
526
+ sage: rels = [y1^2 - (x1^3 + a*x1 + b),
527
+ ....: y2^2 - (x2^3 + a*x2 + b),
528
+ ....: y3^2 - (x3^3 + a*x3 + b)]
529
+ ...
530
+ sage: Q = R.quotient(rels)
531
+ sage: def op(P1, P2):
532
+ ....: x1,y1 = P1; x2,y2 = P2
533
+ ....: lam = (y1 - y2)/(x1 - x2); nu = y1 - lam*x1
534
+ ....: x3 = lam^2 - x1 - x2; y3 = -lam*x3 - nu
535
+ ....: return (x3, y3)
536
+ sage: P1 = (x1,y1); P2 = (x2,y2); P3 = (x3,y3)
537
+ sage: Z = op(P1, op(P2,P3)); W = op(op(P1,P2),P3)
538
+ sage: (Q(Z[0].numerator()*W[0].denominator() -
539
+ ....: Z[0].denominator()*W[0].numerator())) == 0
540
+ True
541
+ sage: (Q(Z[1].numerator()*W[1].denominator() -
542
+ ....: Z[1].denominator()*W[1].numerator())) == 0
543
+ True
544
+ sage: def lcm_upto(B):
545
+ ....: return prod([p^int(math.log(B)/math.log(p))
546
+ ....: for p in prime_range(B+1)])
547
+ sage: lcm_upto(10^2)
548
+ 69720375229712477164533808935312303556800
549
+ sage: LCM([1..10^2])
550
+ 69720375229712477164533808935312303556800
551
+ sage: def pollard(N, B=10^5, stop=10):
552
+ ....: m = prod([p^int(math.log(B)/math.log(p))
553
+ ....: for p in prime_range(B+1)])
554
+ ....: for a in [2..stop]:
555
+ ....: x = (Mod(a,N)^m - 1).lift()
556
+ ....: if x == 0: continue
557
+ ....: g = gcd(x, N)
558
+ ....: if g != 1 or g != N: return g
559
+ ....: return 1
560
+ sage: pollard(5917,5)
561
+ 61
562
+ sage: pollard(779167,5)
563
+ 1
564
+ sage: pollard(779167,15)
565
+ 2003
566
+ sage: pollard(4331,7)
567
+ 1
568
+ sage: pollard(4331,5)
569
+ 61
570
+ sage: pollard(187, 15, 2)
571
+ 1
572
+ sage: pollard(187, 15)
573
+ 11
574
+ sage: def ecm(N, B=10^3, trials=10):
575
+ ....: m = prod([p^int(math.log(B)/math.log(p))
576
+ ....: for p in prime_range(B+1)])
577
+ ....: R = Integers(N)
578
+ ....: # Make Sage think that R is a field:
579
+ ....: R.is_field = lambda : True
580
+ ....: for _ in range(trials):
581
+ ....: while True:
582
+ ....: a = R.random_element()
583
+ ....: if gcd(4*a.lift()^3 + 27, N) == 1: break
584
+ ....: try:
585
+ ....: m * EllipticCurve([a, 1])([0,1])
586
+ ....: except ZeroDivisionError as msg:
587
+ ....: # msg: "Inverse of <int> does not exist"
588
+ ....: return gcd(Integer(str(msg).split()[2]), N)
589
+ ....: return 1
590
+ sage: set_random_seed(2)
591
+ sage: ecm(5959, B=20)
592
+ 101
593
+ sage: ecm(next_prime(10^20)*next_prime(10^7), B=10^3)
594
+ 10000019
595
+ sage: p = 785963102379428822376694789446897396207498568951
596
+ sage: E = EllipticCurve(GF(p), \
597
+ ....: [317689081251325503476317476413827693272746955927,
598
+ ....: 79052896607878758718120572025718535432100651934])
599
+ sage: E.cardinality()
600
+ 785963102379428822376693024881714957612686157429
601
+ sage: E.cardinality().is_prime()
602
+ True
603
+ sage: B = E([
604
+ ....: 771507216262649826170648268565579889907769254176,
605
+ ....: 390157510246556628525279459266514995562533196655])
606
+ sage: n=670805031139910513517527207693060456300217054473
607
+ sage: r=70674630913457179596452846564371866229568459543
608
+ sage: P = E([14489646124220757767,
609
+ ....: 669337780373284096274895136618194604469696830074])
610
+ sage: encrypt = (r*B, P + r*(n*B))
611
+ sage: encrypt[1] - n*encrypt[0] == P # decrypting works
612
+ True
613
+ sage: T = lambda v: EllipticCurve(v
614
+ ....: ).torsion_subgroup().invariants()
615
+ sage: T([-5,4])
616
+ (2,)
617
+ sage: T([-43,166])
618
+ (7,)
619
+ sage: T([-4,0])
620
+ (2, 2)
621
+ sage: T([-1386747, 368636886])
622
+ (2, 8)
623
+ sage: r = lambda v: EllipticCurve(v).rank()
624
+ sage: r([-5,4])
625
+ 1
626
+ sage: r([0,1])
627
+ 0
628
+ sage: r([-3024, 46224])
629
+ 2
630
+ sage: r([-112, 400])
631
+ 3
632
+ sage: r([-102627, 12560670])
633
+ 4
634
+ sage: def cong(n):
635
+ ....: G = EllipticCurve([-n^2,0]).gens()
636
+ ....: if len(G) == 0: return False
637
+ ....: x,y,_ = G[0]
638
+ ....: return ((n^2-x^2)/y,-2*x*n/y,(n^2+x^2)/y)
639
+ sage: cong(6)
640
+ (3, 4, 5)
641
+ sage: cong(5)
642
+ (3/2, 20/3, 41/6)
643
+ sage: cong(1)
644
+ False
645
+ sage: cong(13)
646
+ (323/30, 780/323, 106921/9690)
647
+ sage: (323/30 * 780/323)/2
648
+ 13
649
+ sage: (323/30)^2 + (780/323)^2 == (106921/9690)^2
650
+ True
651
+ """