WNEC Math 377
Number Theory
Prof. Caleb Shor
Spring 2010
Sage is a free open source alternative to Magma, Maple, Mathematica and Matlab. You can download and install Sage on your computer or use Sage through a browser like you are doing right now.
With Sage, we can do basic arithmetic pretty easily. To evaluate a command, put the cursor in the cell, then hold down shift and hit enter.



The underscore (_) gives the most recently output value, much like the 'Ans' command on a TI graphing calculator.


Any text entered on a line after the pound symbol (#) is a comment and is not evaluated by Sage.

We can assign values to variables using the equal (=) sign.




The up and down arrows will move you from one cell to another. To delete a cell, just backspace all of the characters out of it. To insert a cell above a prior cell, hover the mouse just above the top of the cell so that a blue bar appears. Click on that blue bar to create the new cell.

Here's how we compare numbers using <, >, and ==.




Sage has certain variable names reserved, like e and pi. Sage stores exact values for these quantities. For numerical approximations, use the function 'n'.





Need help with a specific function? Just type that function name and follow with a question mark.
Hit "Help" in the upper right of this page for help with finding specific functions.

Here is one of the examples from the end of the help file for plot3d. You should be able to move the graph around by clicking and dragging.

Say we want to do arithmetic modulo n. First, we have to tell Sage that we're working mod n using the Integers() command.









Sometimes, Sage can't compute something. For instance, if we're working modulo 10 and ask for the inverse of 2, an error should appear because there is no inverse of 2 modulo 10. (Equivalently, there are no integers x,y such that 2x10y=1 because the gcd of 2 and 10 is 2.)





Speaking of the greatest common divisor, we have the gcd command in Sage.


The command euler_phi(n) computes the Euler phifunction of n.






Suppose we need an approximation of pi again.

Oops  we gave n a value just above, so it no longer represents the function that gives numerical approximations.
We can reset the value of any variable using the reset command. reset() will reset all defined variables. reset('n') will reset the value of a particular variable.


Now, for primes. Sage has primality testing built in with the is_prime() function. It also has the next_prime() function, which finds the first prime greater than or equal to a particular number.






We now have all the tools we will need to perform encryption and decryption with the RSA cryptosystem!
Here are the steps.
Setup
Encryption
Decryption
