Section 4.2 Going Modulo First
Okay, that's all fun. But we need power, too. Here's an example of such power. Even though I'm not physically present, I can do amazing computations! Let me computexxxxxxxxxx
%time 2^1000000000 % 3
Sage note 4.2.1. Timing your work.
In a Sage worksheet, putting %time
before a command tells you how long it took. Putting %timeit
instead runs the command many times and gives a βbest ofβ timing. (This does not universally work in the embedded cells in the web version of this book.)
Sage note 4.2.2. Numbers too big for a computer.
If I add one more zero, it will throw a very nasty error, like MemoryError: failed to allocate 1250000024 bytes
, because things are too big. We can quickly go beyond the bounds of what our computers can do in number theory!
Fact 4.2.3.
If
Proof.
mod(x,m)
syntax:
xxxxxxxxxx
print(mod(2,3)^1000000000)
print(mod(2,3)^1000000000000000000000000000030)
Sage note 4.2.4. Give things names.
We can use the print
function as above with print( mod(2,3)^1000000000 )
to show multiple computations in a cell. Then again, it only prints them to the output, does not save them, and typing print()
a lot can get annoying.
So instead, we can assign our βmodulo integerβ a name, like b
, and then use it to compute. This makes it easy to do lots of interesting tests.
xxxxxxxxxx
b=mod(2000,31)
b,b^1000,b^2000,b^3000,b^4000
The command in the last line is what prints out in any Sage cell.
Sage note 4.2.5. Making tuples.
In this case, we put commas between things so that all of the stuff in the last row prints out. The output is in parentheses because the commas create a tuple (a special Python way of making a list with certain nice properties).
Sage note 4.2.6. Types matter.
What was computed above is not a trick; I definitely couldn't do b
really is, which confirms that Sage is using modular numbers, not normal integers.
xxxxxxxxxx
b=mod(2000,31)
b, type(b)
In Python, we can ask for the type
of anything.In this case, we asked to output b
and then its type, which is definitely not an ordinary integer, and can be manipulated much more efficiently.