Divisibility and Primes

Download Report

Transcript Divisibility and Primes

Divisibility and Primes
ICS 6D
Sandy Irani
Evenly Divides
• x evenly divides y if y =m·x for some integer m
– Denoted: x|y
– y is an integer multiple (or just “multiple”) of x
– x is a factor of y
Primes and Composites
• A number p is prime if
– p is an integer and p > 1
– The only (positive) factors of p are 1 and p.
• If n > 1 is not prime, it is composite
– n has a positive factor other than 1 or n.
Fundamental Theorem of Arithmetic
• Every integer n > 1 can be expressed as a
product of primes. Prime factorization is
unique up to ordering.
– Prime factorization of n usually written in nondecreasing order:
124 =
Fundamental Theorem of Arithmetic
– Prime factorization of 360:
Exponential Notation for
Prime Factorization
• Write each distinct prime in the prime
factorization in increasing order.
• The multiplicity of a prime (# of times it
appears) is written in the exponent.
124 =
360 =
Prime Factorization and Divisibility
• If you have the prime factorization for x and y,
can easily determine if x divides y:
• Example: does 90 divide 1260?
• 1260 = 22 · 32 · 5 · 7
• 90 = 2 · 32 · 5
Prime Factorization and Divisibility
• 1260 = 22 · 32 · 5 · 7
• 40 = 23· 5
• 22 = 2 · 11
LCM and GCD
• Two positive integers, x and y.
– The least common multiple of x and y, lcm(x,y), is
the smallest integer that is a multiple of x and a
multiple of y.
– The greatest common divisor of x and y, gcd(x, y)
is the largest integer that divides both x and y.
• x and y are relatively prime if gcd(x, y) = 1
Prime Factorization and LCM
• If you have the prime factorization for x and y,
can easily determine if lcm(x, y):
44 = 22 · 11
90 = 2 · 32 · 5
Prime Factorization and GDC
• If you have the prime factorization for x and y,
can easily determine if gcd(x, y):
1320 = 23 ·3 · 5 · 11
1800 = 22 · 32 · 5
Factoring: Brute-force algorithm
• Input: integer N > 1
• Output: (x,y) such that x > 1, y > 1 and xy = N
or “Prime” if N is prime
• For x = 2 to N-1
– If x evenly divides N
• Return(x, N/x)
• Return(“Prime”)
Factoring: slightly better algorithm
• Input: integer N > 1
• Output: (x,y) such that x > 1, y > 1 and xy = N
or “Prime” if N is prime
• For x = 2 to N-1 𝑁
– If x evenly divides N
• Return(x, N/x)
• Return(“Prime”)
If N is composite than it as
at least one factor that is at
most 𝑵
Factoring: slightly better algorithm
• Input: integer N > 1
• Output: (x,y) such that x > 1, y > 1 and xy = N
or “Prime” if N is prime
• For x = 2 to N-1 𝑁
– If x evenly divides N
• Return(x, N/x)
• Return(“Prime”)
If N has 200 digits
𝑵 has 100 digits
If inner loop takes .1 ns
Algorithm take 3 x 1082 years
Time proportional to the value
of N, not the number of digits
Factoring and Primality Testing
• Factoring
• Input: integer N > 1
• Output:
– If N is prime:
• “Prime”
– If N is composite:
• x > 1, y > 1, xy = N
Hard: no known algorithm that
Runs in time (# digits)d
• Primality Testing
• Input: integer N > 1
• Output:
– If N is prime:
• “Prime”
– If N is composite:
• “Composite”
Easy: can be solved in time
(# digits)2 (probabilistic)
Finding Prime Numbers
• “Find a 200-digit prime number”
• How to do this?
• Repeat:
– Pick a random 200 digit number N
– Apply primality test to N
• If N is prime, return(N)
• If N is composite, continue.
The number of trials until
success depends on the
density of prime numbers
among all 200 digit numbers
Density of primes: a first step
• Theorem: there are an infinite number of
prime numbers.
– Euclid ~300 B.C.
The Prime Number Theorem
• Theorem: let π(x) be the number of prime
numbers in the range [2,…,x], then
𝜋(𝑥)
lim
𝑥→∞ 𝑥/(ln 𝑥)
=1
𝜋(𝑥)
ln(𝑥)
𝑥
The Prime Number Theorem
• Theorem: let π(x) be the number of prime
numbers in the range [2,…,x], then
𝜋(𝑥)
lim
𝑥→∞ 𝑥/(ln 𝑥)
=1
Interpretation: A randomly chosen number in the
1
range 2,..,x is prime with probability ~
𝑙𝑛(𝑥)
To get a 200 digit prime number,
we need the density of primes in
the range from 10199 to 10200.
ln(x) ~ 2.3·(# digits in x)