UCT Algorithm Circle: 7 April 2011
Outline Primes and Prime Factorisation 1 Primes and Prime Factorisation 2 3 4
Some revision (hopefully) What is a prime number? An integer greater than 1 whose only factors are itself and 1 2, 3, 5, 7, 11, 13, 17, 19, 23, 29... Non-primes are called composite
Some revision (hopefully) What is a prime number? An integer greater than 1 whose only factors are itself and 1 2, 3, 5, 7, 11, 13, 17, 19, 23, 29... Non-primes are called composite
Some revision (hopefully) What is a prime number? An integer greater than 1 whose only factors are itself and 1 2, 3, 5, 7, 11, 13, 17, 19, 23, 29... Non-primes are called composite
Fundamental Theorem of Arithmetic Any integer greater than 1 can be written as a unique product of prime numbers 6936 = 2 3 3 1 17 2 1200 = 2 4 3 1 5 2
Fundamental Theorem of Arithmetic Any integer greater than 1 can be written as a unique product of prime numbers 6936 = 2 3 3 1 17 2 1200 = 2 4 3 1 5 2
Finding primes the slow way Check for factors! def isprime(x): prime = True for factor in range(2, x): if x % factor == 0: prime = False return prime Can be improved by only checking for factors up to square root of number
Finding primes the slow way Check for factors! def isprime(x): prime = True for factor in range(2, x): if x % factor == 0: prime = False return prime Can be improved by only checking for factors up to square root of number
Sieve of Eratosthenes: a mnemonic Sift the Twos and sift the Threes, The Sieve of Eratosthenes. When the multiples sublime, The numbers that remain are Prime.
Sieve of Eratosthenes Invented by Eratosthenes, Greece, c. 200 BC Quickly find all primes up to a limit x Assume 2 to x are prime Take next known prime, and eliminate its multiples from the list Continue for all numbers on the list The remaining numbers are prime!
Sieve of Eratosthenes Invented by Eratosthenes, Greece, c. 200 BC Quickly find all primes up to a limit x Assume 2 to x are prime Take next known prime, and eliminate its multiples from the list Continue for all numbers on the list The remaining numbers are prime!
Implementation def sieve(x): isprime = [False] * (x + 1) for n in range(2, x + 1): if isprime[n] == True: for multiple in range(2 * n, x + 1, n): isprime[multiple] = False allprimes = [] for n in range(2, x + 1): if isprime[n]: allprimes.append(n) return allprimes
Modification to prime factorise Instead of storing False, store a factor To find a prime factors of n, look up n in the list Divide n by this factor, and look up this new value in the list Continue until the resultant value is 1
Outline Primes and Prime Factorisation 1 Primes and Prime Factorisation 2 3 4
Introduction Common example: a clock Numbers wrap around after some modulus Similar to remainder after division 9 + 4 = 13 1 (mod 12) % operator in Python and other languages
Introduction Common example: a clock Numbers wrap around after some modulus Similar to remainder after division 9 + 4 = 13 1 (mod 12) % operator in Python and other languages
Introduction Common example: a clock Numbers wrap around after some modulus Similar to remainder after division 9 + 4 = 13 1 (mod 12) % operator in Python and other languages
Addition and subtraction Work as expected Just add or subtract remainders, then find new remainder Holds for all real numbers
Addition and subtraction Work as expected Just add or subtract remainders, then find new remainder Holds for all real numbers
Multiplication Multiply remainders, then find new remainder Holds for all integers Division is possible, but complicated
Multiplication Multiply remainders, then find new remainder Holds for all integers Division is possible, but complicated
Multiplication Multiply remainders, then find new remainder Holds for all integers Division is possible, but complicated
Outline Primes and Prime Factorisation 1 Primes and Prime Factorisation 2 3 4
Greatest common divisor The largest factor that will divide two numbers Written GCD(a, b) GCD(9, 12) = 3
By prime factorisation Product of all the common prime factors 6936 = 2 3 3 1 17 2 1200 = 2 4 3 1 5 2 GCD(6936, 1200) = 2 3 3 1 = 24 Prime factorisation is expensive for large numbers Try GCD(2305843009213693951, 2147483647) by hand!
By prime factorisation Product of all the common prime factors 6936 = 2 3 3 1 17 2 1200 = 2 4 3 1 5 2 GCD(6936, 1200) = 2 3 3 1 = 24 Prime factorisation is expensive for large numbers Try GCD(2305843009213693951, 2147483647) by hand!
Euclidean Algorithm Described by Euclid c. 300 BC, but not invented by him GCD(a, b) = GCD(b, a mod b) GCD(a, 0) = a Efficient Number of steps required for completion is never more than five times the number of digits (base 10) of the smaller number Usually much faster
Euclidean Algorithm Described by Euclid c. 300 BC, but not invented by him GCD(a, b) = GCD(b, a mod b) GCD(a, 0) = a Efficient Number of steps required for completion is never more than five times the number of digits (base 10) of the smaller number Usually much faster
Euclidean Algorithm Described by Euclid c. 300 BC, but not invented by him GCD(a, b) = GCD(b, a mod b) GCD(a, 0) = a Efficient Number of steps required for completion is never more than five times the number of digits (base 10) of the smaller number Usually much faster
Efficiency of Euclidean Algorithm
Recursive implementation def gcd(a, b): if b == 0: return a else: return gcd(b, a % b)
Outline Primes and Prime Factorisation 1 Primes and Prime Factorisation 2 3 4
Related reading Euler s Sieve More on modular arithmetic Extended Euclidean Algorithm Fermat s Little Theorem
In the lab Primes and Prime Factorisation Implement a function to prime factorise any given number Implement a Sieve of Eratosthenes Modify it to return the prime factors of all numbers up to the limit