https://slides.com/georgelee/ics141-primes/live
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.
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?
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
* 71
* 97
* 123
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
The greatest common divisor of a and b is the largest integer that divides both a and b. We will denote this using gcd(a, b)
What is gcd(18, 42)?
How do we find gcd(a, b)?
Use the prime factorization of the two numbers.
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).
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
Let's do some examples.
Find the following:
gcd(44, 52)
gcd(203, 101)
gcd(390, 72)
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.