# Number Theory: In Context and Interactive

## Section1.5Using Sage for Interactive Computation

This text is advertised as having interactive computation, but so far any computation has been your own. How does digital computation (interactive or not) fit in? We’ll skip ahead slightly here to see how this will work.
In the interactive version of this text, the areas below are called Sage cells, or cells for short. Assuming you’re connected to the internet, this very first cell will use SageMath
6
www.sagemath.org
(usually just called Sage) to check whether a given fraction remains a fraction when reduced, or whether it reduces to an integer. Click “Evaluate” to try it out.
38/19

Again, if you’re viewing this online, go ahead and try changing the numbers, clicking the evaluate button again.
As we go through the text, you’ll see lots of opportunities to use Sage. Sometimes I’ll give you the opportunity to learn a little bit about how to use it in Sage notes, such as the following one.

Sage notes will teach you useful things about basic programming, or more general facts about Sage and Python
7
www.python.org
, the computer language Sage is based on.
Let’s try another computational cell. We haven’t defined prime numbers yet (see Chapter 6), but I figure you know what they are. Here you can check whether an integer is prime.
is_prime(3169)


### Sage note1.5.2.Using commands in Sage cells.

Assuming you are using this book online, you can put any legitimate Sage command in the cells above. (Try integrate(x^3,x) if you know some calculus.) Or you can use these commands in your own Sage worksheet at your local Sage server or with CoCalc, so that you can save your work!
If you are using an offline or hard copy version, I still highly recommend sifting through some of the code and commands; much of it will enlighten the reader. (Then try it out online or on your local computer!)
Finally, let’s test some conductor ideas using Sage. In the cell below, Sage will automatically list all the nonnegative numbers up to $$n$$ that can be written as $$n=ax+by$$ for nonnegative integers $$x$$ and $$y\text{.}$$ The default values are $$a=3,b=4\text{;}$$ you can experiment by changing one or both of these values.
@interact
def _(a=(3,[2..10]),b=(4,[2..10]),n=(20,[10..50])):
list_of_them=list(set([a*x+b*y for x in srange(n/a+1) for y in srange(n/b+1)]))
list_of_them=[item for item in list_of_them if item <= n ];list_of_them.sort()
pretty_print(html("The nonnegative integers up to $n=%s$ which can be"%(str(n))))
pretty_print(html("written as positive combinations of $a=%s$ and $b=%s$ are:"%(str(a),str(b))))
print(list_of_them)

Notice that with the default values above we are definitely getting the same answers as expected from our ‘pencil and paper’ experiments.
Finally, notice that the algorithm I used in the code is very naive – I just listed all possible combinations under a certain size. It would be interesting to use this to try to verify patterns you may have noticed about the precise size of the conductor, and when it exists.