Elementary number theory using Maxima
Prime numbers
You might remember that for any integer greater than 1, is a prime number if its factors are 1 and itself. The integers 2, 3, 5, and 7 are primes, but 9 is not prime because . The command primep() is useful for testing whether or not an integer is prime:
1 2 3 4 5 6 7 8 9 10 |
|
And the command next_prime(n) returns the next prime number greater than or equal to :
1 2 3 4 5 6 7 8 9 10 |
|
Let’s now define a function called primes_first_n() in Maxima to return a list of the first primes, where is a positive integer. Programming in the Maxima language is different from programming in other languages like C, C++, and Java. For example, if your variable is be assigned a number, you don’t need to define whether your variable is of type int, long, double, or bool. All you have to do is use the colon operator “:” to assign some value to a variable, like in the following example.
1 2 3 4 5 6 7 8 9 |
|
Before defining the function primes_first_n(), there are two useful built-in Maxima functions that you should know about. These are append() and last(). The function append() can be used to append an element to a list, whereas last() can be used to return the last element of a list:
1 2 3 4 5 6 7 8 |
|
Below is the function primes_first_n() which pulls together the features of next_prime(n),append(), and last(). Notice that it is defined at the Maxima command line interface.
1 2 3 4 5 6 7 8 9 10 11 |
|
You can also put the above function inside a text file called, say, /home/mvngu/primes.mac with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Like C++ and Java, Maxima also uses “/*” to denote the beginning of a comment block and “*/” to denote the end of a comment block. To load the content of the file/home/mvngu/primes.mac into Maxima, you use the command load(). Let’s load the above file and experiment with the custom-defined function primes_first_n():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Factorizing integers
Integer factorization is about breaking up an integer into smaller components. In number theory, these smaller components are usually the prime factors of the integer. Use the command ifactors() to compute the prime factors of positive integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The prime factors of 10 are and . When you multiply these two prime factors together, you end up with . The expression is called the prime factorization of 10. Similarly, the expression is the prime factorization of 25, and is the prime factorization of 72.
Greatest common divisors
Closely related to integer factorization is the concept of greatest common divisor (GCD). The Maxima command gcd() is able to compute the GCD of two expressions and where this makes sense. These two expressions may be integers, polynomials, or some objects for which it makes sense to compute their GCD. For the moment, let’s just work with integers:
1 2 3 4 5 6 |
|
The GCD of two integers and can be recursively defined as follows:
where is the remainder when is divided by . The above recursive definition can be easily translated to a Maxima function for integer GCD as follows (credit goes to amca for the Maxima code):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Save the above code to a text file and load it first before using the function. Or you can define the function from the Maxima command line interface and proceed to use it:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The extended Euclidean algorithm provides an interesting relationship between , and the pair and . Here is a Maxima function definition courtesy of amca:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Or you can define it from the Maxima command line:
1 2 3 4 5 6 7 8 9 10 |
|
Let’s use the function igcdex() for various pairs of integers and verify the result.
1 2 3 4 5 6 7 8 9 10 11 12 |
|