number_theory1

798 days ago by jozefl

Let's look at modular arithmetic.  The first function we'll look at is the quotient and remainder function.

x//y returns the quotient of x and y.

x%y returns the remainder.

print 7//2 print 7%2 
       

Let's look at modular arithmetic. For a more complete tutorial, see:

buzzard.ups.edu/sage/sage-group-theory-primer.pdf

The first function we'll look at is the quotient and remainder function.

x//y returns the quotient of x and y.

x%y returns the remainder.

mod(7,2) 
       

We can also do the Euler phi function.

euler_phi(9) 
       

And a prime test.

p=22 if is_prime(p): print p,' is prime.' else: print p,' is composite.' f=divisors(p) print 'The factors of ',p,' are: \n',f 
       

Here is an example of some more advanced modular arithmetic. If we wish to deal with Z/nZ we use the command

IntegerModRing(n).

In the example below, we'll use integers mod 97.  We'll also demonstrate how to take the modulus and a test to see if a number is square:

For a number n:

n.modulus()   returns the modulus.

n.is_square()   returns 'True' or 'False'.

 

R = IntegerModRing(97) a = R(2) / R(3) print 'a = ',a print a.rational_reconstruction() b = R(47) print 'b^20052005 = ',b^20052005 print '|b| = ',b.modulus() print 'True/False: b is square. Answer: ',b.is_square()