// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// The Integers
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// The quotient and remainder specified by the
// Division theorem are given by
q, r := Quotrem(65,15);
// You can also use
q := 65 div 15 ; r := 65 mod 15;
q, r;
// The function that computes the gcd can also be applied to a
// set or sequences. The linear combination that gives the gcd
// is computed by XGCD();
// LCM works similarly.
GCD(65, 15);
GCD({65,15,35,95});
d, r, s := XGCD(65, 15);
d; r; s;
d,r,s;
IsDivisibleBy(65,2);
IsPrime(65);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Factorization and Primality
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p := 3;
a := 21354;
n := 234546344363;
Factorization(n);
IsPrime(n);
NextPrime(n);
// For modular arithmetic there are several ways to proceed.
// The first can be a problem as you can see.
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Integers Mod n
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a^n mod p;
Modexp(a, n, p);
// The order in the multiplicative group of Z/n.
Order(a,p); // This gives 0 since p divides a
Order(a,n);
PrimitiveRoot(127);
for i in [1 .. 126] do
3^i mod 127;
end for;
// There are also functions ModInv and Modsqrt. But it is probably best
// to create the ring Z/p; and then you can use a^k for exponentiation
// and inverting (k=-1) and Sqrt(a) for square roots.
A := Integers(p);
// The ! is the coercion operator. It is used to tell Magma that
// you want a to live in the ring A that we created.
p := NextPrime(328941234312732423);
a := 14445432;
n := 23557638567;
A := Integers(p);
a := A!a;
a^n ;
Sqrt(a);
b := a^2;
Sqrt(b);
// Notice we don't have to use the modulus in the next function.
Order(a);
IsPrimitive(a);
// Notice that when we look for a primitive element we give Magma
// the ring A as argument.
PrimitiveElement(A);
// There are no primitive elements mod 60.
PrimitiveElement(Integers(60));
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Chinese Remainder Theorem
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// This solves the linear congruence 5x = 7 mod 8
Solution(5, 7, 8);
// This solves the Chinese Remainder theorem for
// 2x = 2 mod 8; 3x = 1 mod 9; 5x = 3 mod 11
Solution([2,3,5], [2,1,3], [8,9,11]);
// Let's fix that problem with 3x = 1 mod 9.
Solution([2,2,5], [2,1,3], [8,9,11]);