Jordi Cortadella Department of Computer Science Prime number A prime number is a natural number that has exactly two distinct divisors 1 and itself 1 is not prime ID: 269871
Download Presentation The PPT/PDF document "Prime numbers" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Prime numbers
Jordi CortadellaDepartment of Computer ScienceSlide2
Prime number
A prime number is a natural number that has exactly two distinct divisors: 1 and itself.
1 is not prime
2 is prime 6 is not prime 11 is prime 1023 is not prime 2110454939 is prime
Introduction to Programming
© Dept. CS, UPC
2Slide3
Prime number: specification
def is_prime(n):
"""n >= 0.
Returns True if n is prime, and False otherwise. """ Strategy: try all possible divisors from 2 to n-1Check that n is divisible by d: n%d == 0
Introduction to Programming
© Dept. CS, UPC
3Slide4
Is it prime?
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
Introduction to Programming
© Dept. CS, UPC
4Slide5
Prime number
def is_prime(n): """arg
: n (
int
) >= 0. Returns True if n is prime, and False otherwise. """ if n <= 1: return False for d in range(2, n): if n%d
== 0: return
False
return True
Introduction to Programming
© Dept. CS, UPC
5Slide6
Prime number: doing it really fast
If n is not prime, we can find two numbers,a and b, such that:
and with the following property:
There is no need to find divisors up to
.
We can stop much earlier.
Note:
is equivalent to
.
Introduction to Programming
© Dept. CS, UPC
6Slide7
Is it prime?
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
9899
Introduction to Programming
© Dept. CS, UPC
7Slide8
Prime number
def is_prime(n): """arg: n (
int
)
>= 0. Returns True if n is prime, and False otherwise. """ if n <= 1: return False d = 2 while d*d <= n: if n%d == 0: return
False
d += 1
return True
Introduction to Programming
© Dept. CS, UPC
8Slide9
Is there any real difference?
Iterations
Number of bits
Introduction to Programming
© Dept. CS, UPC9Slide10
It makes a differenceIntroduction to Programming
© Dept. CS, UPC10Slide11
Prime factorsWrite a function that prints the decomposition of a number in prime factors
Example: input: 350 output: Factors of 350:
2 5 5 7
Intuitive algorithm:
Try all potential divisors d, starting from 2If divisible by d, divide and try again the same divisorIf not divisible, go to the next divisorKeep dividing until the number becomes 1Introduction to Programming© Dept. CS, UPC11Slide12
Prime factors
n
d
divisible
write3502yes21752no
175
3
no
175
4
no
175
5
yes
5
35
5
yes
5
7
5
no
7
6
no
7
7
yes
71finishIntroduction to Programming© Dept. CS, UPC12The algorithm will never write a non-prime factor. Why ?Slide13
Prime factors
def print_prime_factors(n): """Prints the prime factors of n. For each factor, it prints as many
instances as
the multiplicity
of the factor in n. Pre: n > 1. Example: Factors of 350: 2 5 5 7 """ print('Factors of ', n, ':', sep='', end='') # Divide n by divisors from 2 in ascending order d = 2 while n != 1: if n%d == 0: # Check if divisible print(' ', d, sep='', end ='')
n = n//d else:
d += 1
print
()
# Final end-of-line
Introduction to Programming
© Dept. CS, UPC
13Slide14
ConclusionsMany algorithms exist to check for primality, but few of them are simple and efficient.
Use your knowledge about the problem to figure out how to solve it efficiently.If you want to find all prime numbers up to a limit, use the sieve of Eratosthenes.http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Introduction to Programming
© Dept. CS, UPC
14