Basics of Number Theory

The starting point is primes. There are several ways to get primes.

> nextprime(100);

101

> ithprime(55);

257

> isprime(%);

true

The Euclidean algeorithm is used to find the greatest common divisor. You can get the quotient of each integer by the gcd. The two extra variables are set to these values.

> gcd(27,18);

9

> gcd(27,18,'ap','bp');ap; bp;

9

3

2

You can also get the linear combination that gives the gcd..

> igcdex(45,290,'s','t');

5

> s; t; s*45 + t*290;

13

-2

5

The same can be done with polynomials. You have to specify the variable, too.

> F := x^3-3*x-2; H := x^4-4*x^2-x+2;

F := x^3-3*x-2

H := x^4-4*x^2-x+2

> gcdex(F, H, x, 's','t');

-2+x^2-x

> s; t; s*F + t*H; simplify(%);

x

-1

x*(x^3-3*x-2)-x^4+4*x^2+x-2

-2+x^2-x

A number of functions have two versions, one for polynomials and one for integers. The integer version is preceeded with i (e.g. gcd). Other functions have both real number and integer versions. Again, the integer version is preceeded with i.

> ifactor(1351895612498);

``(2)*``(375318049)*``(1801)

In ifactors(), the first term is -1 if the input is negative. The other terms are [prime ,power].

> ifactors(120);

[1, [[2, 3], [3, 1], [5, 1]]]

The q is optional and it receives the quotient. iquo works the other way returning the quotient, and (optionally) setting an input variable to the remainder.

> irem(29,7,'q'); q;

1

4

You can solve equations for integer solutions.

> isolve(3*u-4*v=7);

{v = 2+3*_Z1, u = 5+4*_Z1}

>

>