Prime Numbers

https://slides.com/georgelee/ics141-primes/live

We know prime numbers

Prime Numbers

An integer p is called prime if the only positive factors of p are 1 and p. Any positive number greater than 1 that is not prime is called composite.

 

Also, two numbers are relatively prime if their only common factor is 1.

Composite Numbers

If a number n is composite, then its prime factors are no larger than √n.

 

How might we determine if a number n is prime?

Trial Division

Brute force algorithm for determining if a number n is prime.

 

for each prime number p < √n
    if n mod p == 0
      return false

return true

Are these numbers prime?

* 71

* 97

* 123

Wait a Minute ...

How do we find prime numbers less than √n ???

Sieve of Erastothenes

Algorithm for finding a list of prime numbers less than n.

 

primes = list of all numbers from 2 to n.

for p in primes
  for number in primes
    if p != number and number mod p == 0
      delete number from primes

 

return primes

Greatest Common Divisor

Greatest Common Divisor

The greatest common divisor of a and b is the largest integer that divides both a and b. We will denote this using gcd(ab)

 

What is gcd(18, 42)?

Brute Force Algorithm

How do we find gcd(ab)?

Prime Factorization

Use the prime factorization of the two numbers.

Euclidean Algorithm

Trial division can be expensive

Euclidean Algorithm

Breaks down finding a gcd of two numbers to smaller problems.

 

We will use the following fact.

 

Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r).

Euclidean Algorithm

q = a div b
r = a mod b

while r > 0
  a = b
  b = r
  q = a div b
  r = a mod b

return b

Euclidean Algorithm

Let's do some examples.

 

Find the following:

 

gcd(44, 52)

gcd(203, 101)

gcd(390, 72)

Linear Combinations

The gcd of two numbers can be expressed as a combination of the two original numbers. That is:

 

gcd(a, b) = sa + tb for some integers s and t.

Primes

By George Lee

Primes

  • 835