OurFirstMathematicaNotebook
Many topics in number theory are best learned using Wolfram Mathematica or a comparable computer algebra system such as SageMath or Mathematica. (Some, but not all, of these tasks can be formed on your calculator or Matlab.) Some of our homework assignments will assume you have access to such computational resources either
1
. Wolfram Alpha, accessed through a browser;
2
. Mathematica, at a UW campus workstation;
3
. Mathematica, from off campus, logged in remotely to a UW computer lab;
4
. Mathematica, installed on your computer using a student license provided by UW; or
5
. SageMath or other equivalent software.
See https://uwyo.teamdynamix.com/TDClient/1940/Portal/Requests/ServiceDet?ID=9596 for help with accessing Wolfram Software.
Here we demonstrate the use of a Mathematica notebook, which allows us to organize, save and share our work in a formatted document, using headings and text documentation in addition to Mathematica code. All Mathematica documents that I show you in our class, including this one, will be shared with you so that you can imitate and re-use these code segments in your own study and homework solutions. Let’s demonstrate the use of Mathematica through a series of computational examples.
To compute the algebraic expression shown, enter as “code”, then shift-enter:
To compute the algebraic expression shown, enter as “code”, then shift-enter:
In[]:=
3*(1+17^11)
In[]:=
3/4 + 11/13
Some computations use specialized commands whose arguments are enclosed in brackets:
In[]:=
Factorial[22]
Solve a quadratic equation for . Omit the “Clear[x];” command if the variable “x” hasn’t yet been assigned.
x
In[]:=
Solve[x^2+13*x+2==0, x]
Sometimes an attempted computation like this fails because your variable name (in this case x) has already been assigned a value. One remedy is to first clear the variable name, and try again:
In[]:=
Clear[x]; Solve[x^2+13*x+2==0, x]
If you don’t know the Mathematica command to compute something, use instead “NaturalLanguageInput”, which will first try to translate your command into Mathematica code. Mathematica will try to interpret your natural language as a command (which you should first verify) and then proceed to give the answer (when you click on the boxed equation symbol “=” on the left):
It will be your responsibility to check that Mathematica has correctly interpreted your “natural language” (it doesn’t always!). Once you have learned the correct Mathematica code, you can use that directly.
We will often perform operations using modular arithmetic. This example uses modular arithmetic:
This example asks us to solve a congruence. We apparently need to click on the equation symbol (highlighted equals) to compute:
Separate multiple commands using semicolons. Commands ending in a semicolon will not result in displayed output; only the last command (without a semicolon) will be shown:
In[]:=
a=2^5+1; b=2^a+1; c=b^2+1
For use in class demonstration, I may prefer a different approach which displays all values of a,b,c, for example:
In[]:=
a=2^5+1; b=2^a+1; c=b^2+1; Print["(a,b,c) = (",a,",",b,",",c,")"]
In some demonstrations, I may want to show the full algebraic expression leading to the final value:
In[]:=
Print[HoldForm[(2^HoldForm[2^5+1]+1)^2+1]," = ",c]
Although these practices are helpful when making class demonstrations, I am not expecting you to adopt any of these advanced features when using Mathematica for your own purposes! A simpler choice of syntax for this was shown above; or instead, use something like:
In[]:=
(2^(2^5+1)+1)^2+1
Also, “%” represents “the previous expression”; so you could show all intermediate steps by avoiding semicolons, and ending each line with “enter” (NOT “shift-enter”):
In[]:=
2^5+12^%+1%^2+1
Mathematica is a very extensive computer algebra system, most of which we will not see or use in this course. Let’s however look at some typical computations we will be using, which I will introduce using the natural language feature:
Let’s try to list all primes between a googol and a googol+1000 (there can’t be very many in this interval). Mathematica’s natural language feature tries to do this the wrong way and fails:
Instead, we test each integer in the required range, and individually test each one for primality. This gives us the complete list of primes in the desired interval (just two of them). We can use a “for loop”:
In[]:=
googol=10^100; For[n=googol, n<=googol+1000, n++, If[PrimeQ[n], Print[n," is prime"]]]
Similarly, let’s use a “while loop” to list all prime numbers less than 1000 of the form +1:
2
n
In[]:=
n=0;While[n^2<1000, If[PrimeQ[n^2+1], Print[n^HoldForm[2]+1," = ",n^2+1," is prime"]]; n++]
Next, let’s give an example of a procedure (i.e. function or subroutine) in Mathematica, which accepts one positive integer as input (note the underscore in specifying this variable as input, i.e. n_ ). Its output is a list the ways that can be expressed as a sum of two integer squares. In order to suppress duplicates, we will ask for pairs such that where . It is important to realize that this naive code only works for “small” values of . Later in the course we will see how to express a large integer (possibly hundreds of digits long) as a sum of two squares, whenever such an expression exists (and why we would care about doing so).
n
n
(x,y)
n+
2
x
2
y
0≤x≤y
n
n
In[]:=
sum2squares[n_] := (x=0; y=Sqrt[n]; While[x<=y, y=Sqrt[n-x^2]; If[IntegerQ[y], Print[x^HoldForm[2]+y^HoldForm[2]," = ",x^2+y^2]]; x++])
In[]:=
sum2squares[109]
In[]:=
sum2squares[49]
In[]:=
sum2squares[52200]
In[]:=
sum2squares[52201]
In[]:=
sum2squares[52202]
Use a “for loop” to write each as a sum of two squares whenever possible:
n∈{52200,52201,…,52210}
In[]:=
For[n=52200, n<=52210, Print["Write ",n," as a sum of two squares"]; sum2squares[n]; n++]