Karl-Dieter Crisman, Gordon College
Sage Education Days 3
This Sage worksheet is partly based on materials developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071) and was developed for the Sage Education Days 3 as part of the UTMOST Grant (several NSF DUE grants).
Elementary number theory shows up in various places in the curriculum.
So why use Sage (or any other computer system) in these contexts?
Since Sage began life as a project in algebraic and analytic number theory (and this continues to be a big emphasis), it is no surprise that functionality in this area is extremely comprehensive.
The rest of this talk will show various basic topics that I cover in my own number theory course, highlighting how I use Sage to discuss them, especially to visualize them. Along the way, there will be plenty of '@interacts' or 'Sagelets' to (hopefully) inspire.
One of the most important things in Sage is that the ring of integers modulo n is available without further effort, so one can do examples in modular arithmetic very easily.
For instance, we can create a number in \mathbb{Z}_n. The 'type' command tells us that a is not a regular integer.
2 <type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'> 1 1 2 <type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'> 1 1 |
In fact, "mod(a,n)" will create an integer modulo n, represented as the least nonnegative residue of that integer a.
Note two things about the previous cell.
This highlights two points about how to use Sage.
Here's another example of this. Recalling the basic programming construct called a loop, we can verify this for all a in the integers modulo p.
0 0 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 0 0 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 |
Notice that 'Integers(11)' gave us an algebraic object which is the ring of integers modulo the prime ideal generated by the element 11. This is handy later.
This works for much bigger numbers too, of course. We won't want to print out all the integers here, but we can check things, at least!
|
|
If I'm going to do things with my integers modulo p, I should name them. I can name individual elements, too, as we saw above.
121757494574852613234843425488130908318535941046149934163639623072554758\ 721607163112727289400782113260456010519835894142669183867556534794718528\ 46103058479931430533099115465341959637292476842332898283 1 12175749457485261323484342548813090831853594104614993416363962307255475872160716311272728940078211326045601051983589414266918386755653479471852846103058479931430533099115465341959637292476842332898283 1 |
Here the student sees that it's the last line that counts when it comes to printing.
667341860419940833796129083121003719719303707438654397250300236758579316\ 812430945254713755146961572305624053487037532854247248289759595685250040\ 511759445620464084950052304753065232782552186716875365 667341860419940833796129083121003719719303707438654397250300236758579316812430945254713755146961572305624053487037532854247248289759595685250040511759445620464084950052304753065232782552186716875365 |
A slight interlude:
Whenever you encounter a new object, you should try tab-completion to see what you can do to it. Let's try it here, and pick one (hopefully one that won't be too long!).
|
|
Here's one that sounds interesting.
|
File: /home/sageserver/sage/devel/sage/sage/rings/ring.pyx Type: <type ‘builtin_function_or_method’> Definition: Zp.zeta(n=2, all=False) Docstring:
|
And we use it to find the fifth roots of unity in this field.
[80199770388563324100334548626345240081294273289109866436996652525328268\ 652922508892946068538641538316054373187019168781211876036849531337824832\ 226216684677717580165592175377569174402189281574130719978, 698397838955722862975688344850250735578853643480710617154654770618734003\ 597949893674234076839712993618172452139471823440907398433670761970163225\ 41936552333837227080274674865687645877633828974738751695, 574074442191990614988012986723235901632385922015745724828366190256768695\ 370076093153868008522043375878052492508966514679705854505181577011158937\ 49407382500580682168292929753154872678880962261809848942, 419366418775396766525315677232493679171086389871318795216062437418144020\ 953437245408446072129992970648162308286210640262632966028055359700916965\ 70138772210094329631491849238240260893562065879302446837] [80199770388563324100334548626345240081294273289109866436996652525328268652922508892946068538641538316054373187019168781211876036849531337824832226216684677717580165592175377569174402189281574130719978, 69839783895572286297568834485025073557885364348071061715465477061873400359794989367423407683971299361817245213947182344090739843367076197016322541936552333837227080274674865687645877633828974738751695, 57407444219199061498801298672323590163238592201574572482836619025676869537007609315386800852204337587805249250896651467970585450518157701115893749407382500580682168292929753154872678880962261809848942, 41936641877539676652531567723249367917108638987131879521606243741814402095343724540844607212999297064816230828621064026263296602805535970091696570138772210094329631491849238240260893562065879302446837] |
Are these really fifth roots of unity? We use the list comprehension (set-builder) notation this time - another opportunity.
[1, 1, 1, 1] [1, 1, 1, 1] |
Luckily, it checked out.
I can also try to make this interactive, to remove the 'programmingese'.
Click to the left again to hide and once more to show the dynamic interactive window |
Incidentally, it seems like there is either a bug here or a documentation error. Shouldn't ONE be a fifth root of unity?
This also exemplifies that it's important to know the level you are aiming at.
Similarly, what background do they have mathematically?
So the previous example may seem like it wasn't elementary, but let's think about the connection.
And that is something we constantly should emphasize - that things are different modulo p!
There is a lot more we can access at the elementary level, such as
A good way to use Sage in this context is to allow students to experiment with pencil and paper first, then use Sage to see whether patterns they discover hold true before one attempts to prove them.
2 (2, 3) True 1 2 (2, 3) True 1 |
By the way, there are lots of pedagogical opportunities to improve Sage in number theory. For instance, someone just added the Jacobi symbol to fill in the gap between Legendre and Kronecker symbols.
Another place where someone is working is in the 'naive' modular solver.
[(4, 2), (4, 6), (4, 9), (4, 13)] [(4, 2), (4, 6), (4, 9), (4, 13)] |
All of these capabilities makes it fairly easy to construct elementary cryptographic examples as well.
Here is a standard example of a Diffie-Hellman key exchange, for instance.
17686401212520143419 2 11357069553300816796 6155949584214785623 7166503060918115829 7166503060918115829 17686401212520143419 2 11357069553300816796 6155949584214785623 7166503060918115829 7166503060918115829 |
So Alice picks n and sends q^n, Bob picks m and sends q^m, and then they jointly have the key
The last thing verifies that the private keys they get are the same.
Most number-theoretic functions are pretty easy to find with tab-completion. Let's try this now - what's something we should have available?
|
|
For those who are interested, more advanced number-theoretic objects are easy to come by.
In the first example, K is the field extension \QQ(\sqrt{-14}), where the symbol 'a' plays the role of \sqrt{-14}; we discover several basic facts about K in the next several cells.
Number Field in a with defining polynomial x^2 + 14 Number Field in a with defining polynomial x^2 + 14 |
-56 4 True -56 4 True |
Various zeta functions are also available; here is a complex plot of the Riemann zeta.
|
|
Sage supports various more advanced cryptographic procedures as well as some basic pedagogical ones natively. This example is adapted from the documentation. The pycrypto module has serious versions.
Simplified DES block cipher with 10-bit keys Simplified DES block cipher with 10-bit keys |
[0, 1, 0, 0, 1, 1, 0, 1] [0, 1, 0, 0, 1, 1, 0, 1] |
|
|
Now it's time to start visualizing mathematics. First, let's see the traditional way. Look at powers of a\text{ mod }(p); students are to look for patterns.
Click to the left again to hide and once more to show the dynamic interactive window |
Did you get a chance to come up with something? What do you think? Did you come up with some potential theorems? Notice that we use a little string formatting - totally optional, of course.
Now, how many theorems do you see here?
|
|
This is a graphic giving the various powers of integers modulo p as colors, not numbers. The columns are the powers, so the first column is the zeroth power (always 1) and the second column gives the colors for the numbers modulo the given prime (first power). The a-1 row and b column gives the color corresponding to (a-1)^b mod (p). That means the first (0th) column is the color for 1 and the second (1th) column gives the colors of each element of \mathbb{Z}_p. For instance, (3,4) corresponds to 2^4 mod (7) in the initial example below.
Theorems:
It's certainly possible to have students or you now try changing p to see what else happens. After all, they won't necessarily see any patterns immediately. Let's try this.
But with just a little work, we can make this interactive. Now the programming is hidden, and it's far more interesting to the students. This is my pedagogy.
Click to the left again to hide and once more to show the dynamic interactive window |
My contention is that combining interaction with visualization is a great way to teach this discipline.
Note that if you don't like the colors, you can change the word in the quotes after the word 'cmap'; if you get rid of that, it will be a grayscale plot. Some others you could try are 'Oranges' or 'hsv' or \ldots
Another nice exercise is to start trying to find patterns in the primes p such that
Click to the left again to hide and once more to show the dynamic interactive window |
Another very useful object is the prime counting function \pi(x). This comes with its own custom plotting.
25 25 |
A very nice aspect of Sage is combining several aspects of mathematics together. It can be very eye-opening to students to see analytic aspects of number theory early on.
|
|
We'll return to this later.
|
|
I think that one of the most powerful ways to use Sage is to illuminate visual proofs that are often hard to understand with a single static picture. The following is part of the proof of quadratic reciprocity.
Recall the statement of quadratic reciprocity. For odd primes p and q, we have that
Proof:
We will use a criterion of Eisenstein's that I introduced in class (see this article).
Recall that p and q are odd primes. Now let
The key will be geometrically interpreting \left\lfloor\frac{qe}{p}\right\rfloor as being the biggest integer less than \frac{qe}{p}. The following features are present in the next graphic, which should clarify how we'll think of it geometrically.
It should be clear that each blue stack has the same height as \left\lfloor\frac{qe}{p}\right\rfloor for some even e.
Click to the left again to hide and once more to show the dynamic interactive window |
What we will now do is try to convince ourselves that the number of blue points has the same parity as the total number of (positive) points in and on the green box under the dotted line. If we can do that, the following steps finish the proof of quadratic reciprocity.
This would definitely complete the theorem! And I prove this in the rest of class.
Why is this better?
Analytic number theory is another great place to look - which makes sense, because a lot of what it does is make computations related to L-functions... here's another excerpt, with some final graphics. Of course, this is toward the end of class.
Somewhat remarkably, the first people we know of compiling substantial data about this are Gauss and Legendre. See the handout.
This should sound 100% crazy! But Gauss and Legendre were no fools, and the accuracy is astounding. Let's call Li(x)=\int_0^\infty \frac{dt}{\ln(t)} (yes, this is a convergent integral).
Click to the left again to hide and once more to show the dynamic interactive window |
Notice how much closer Li(x) is than the x/\ln(x) estimate. It's usually closer by several orders of magnitude.
Click to the left again to hide and once more to show the dynamic interactive window |
Here's an even better estimate I talk about the last day or two, while students are working on take-home projects and don't want homework in addition.
Click to the left again to hide and once more to show the dynamic interactive window |
|
|
And here we see Riemann's exact formula for \pi(x), through the zeros on the critical line up to t=150.
|
|
That's just for fun, really. Thank you!
|
|