The MAGMA command to compute all the divisors of an integer
is Divisors(n);
For example, Divisors(15);
returns [1,3,5,15].
The remainder and quotient of
divided by
is
the MAGMA command Quotrem(n,m);.
For example, Quotrem( 11, 2);
returns 5 1.
MAGMA does not have a base
conversion directly.
One must use it's p-adic numbers package to convert from
decimal to base
and this only works when
is
a prime. The command Zp<p> := pAdicRing(m);
creates the data needed to work with base
notation,
when
is a prime. The integer
indicates the
number of
-ary digits which will be used.
The commands r := Zp!a; Coefficients(r); returns the
expansion of
in base
out to
places.
For example,
Zp<p> := pAdicRing(5);r := Zp!127;,
Coefficients(r);
returns
[ 2, 0, 0, 1 ] This is read least significant to most
significant digits, left to right:
the
is the
's place, those to the right of the
decimal point are the
's place,
's places, and so
on. The output tells us
.
The greatest common divisor of
and
is given by
GreatestCommonDivisor(m, n); .
For example, GreatestCommonDivisor(10005,50001);
returns 3.
To find
such that
, use the GAP
command XGCD([ m,n]);. For example,
XGCD([108,801]);
returns
To find the least common divisor of
and
,
use the GAP command LCM([ m, n]); .
For example, LCM([ 155,15]);
returns 255 .
The Euler totient function
is given by the
command EulerPhi( n);. For example,
EulerPhi(2^{13}-1); returns 8190.
The next prime after
is given by the command
NextPrime(n);.
For example, NextPrimeInt(100);
returns 101.
The prime factorization of
is given by the command
Factorization(n); . For example,
Factorization(100);
returns [ <2, 2>, <5, 2> ].
Program the Lucas-Lehmer sequence
,
, ....
, by typing
function lucaslehmer(n) if n eq 1 then return 4; end if; if 1 lt n then return lucaslehmer(n - 1)^2 - 2; end if; return 0; end function;
We turn to a method to test the primality of
special numbers of the form
, where
is a prime.
The next result is included to
illustrate the type of special methods known to
test the primality of integers of special forms.
The proof is omitted. See [Br].
Note that we need not and should not
compute
directly, which grows very fast (in
fact, it grows faster than
).
For example, when testing for the primality of
,
we do not compute
and see if
;
rather we compute
and see if it
is zero. The point is that the
new sequence
Lucas-Lehmer test:
Let
, where
is a prime.
For
, if
, then
is a prime.
(b) What is the largest prime you can find on your computer using this test?
(c) Rewrite the code so that the
procedure computes
and
.
(d) Find the smallest
for which your computer
takes more than 1 minute (by your watch) to compute
.
The Lucas-Perrin sequence
function lucasperrin(n) if n eq 1 then return 0; end if; if n eq 2 then return 2; end if; if n eq 3 then return 3; end if; if 1 lt n then return lucasperrin(n - 2) + lucasperrin(n - 3); end if; return -1; end function;
(a) Compute
.
(b) What is the largest ``prime candidate'' you can find on your computer 1.11?
The Chinese remainder theorem commands
give us the
minimal solution
of
,
,
...,
.
The command for the Chinese remainder theorem
is
CRT([a1,a2,...,ak],[n1,n2,...,nk]);.
For example, CRT([1,2],[5,7]); returns 16 .
The multiplicative order of
mod
in MAGMA is given by
the commands R := ResidueClassRing(m); Order(R!a); .
For example,
R := ResidueClassRing(17); Order(R!11);
returns 16.
(a) the order of
mod
,
(b) the order of
mod
.
R := ResidueClassRing(m); r:=R!a; r^e;
returns the
power of
modulo
.
The primitive root mod
is given by
PrimitiveRoot(m);.
The discrete log is given by
Log(GF(m)!a,GF(m)!b); (
prime).
For example,
PrimitiveRootMod(7); and LogMod( 64, 5,97);
returns 3 and 12,
respectively.
(a) Find the
discrete log of
to base
mod
, if it exists.
(b) Is
a prime?
Is
a primitive root mod
?
Find the
discrete log of
to base
mod
, if it exists
(ans:
),
In MAGMA, type
ContinuedFraction(r);, where
is real,
to get the continued fraction of
.
For example, ContinuedFraction(Sqrt(2));
returns [ 1, 2, 2, ...].