Prime numbers Jordi Cortadella Department of Computer Science
Prime numbers 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 2 6 11 1023 2110454939 Introduction to Programming is is is not prime not prime © Dept. CS, UPC 2
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-1 Check that n is divisible by d: n%d == 0 Introduction to Programming © Dept. CS, UPC 3
Is it prime? 0 10 20 30 40 50 60 70 80 90 Introduction to Programming 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72 82 92 3 13 23 33 43 53 63 73 83 93 4 14 24 34 44 54 64 74 84 94 5 15 25 35 45 55 65 75 85 95 © Dept. CS, UPC 6 16 26 36 46 56 66 76 86 96 7 17 27 37 47 57 67 77 87 97 8 18 28 38 48 58 68 78 88 98 9 19 29 39 49 59 69 79 89 99 4
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 5
Prime number: doing it really fast • Introduction to Programming © Dept. CS, UPC 6
Is it prime? 0 10 20 30 40 50 60 70 80 90 Introduction to Programming 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72 82 92 3 13 23 33 43 53 63 73 83 93 4 14 24 34 44 54 64 74 84 94 5 15 25 35 45 55 65 75 85 95 © Dept. CS, UPC 6 16 26 36 46 56 66 76 86 96 7 17 27 37 47 57 67 77 87 97 8 18 28 38 48 58 68 78 88 98 9 19 29 39 49 59 69 79 89 99 7
Prime number import math def is_prime(n): """arg: n (int) >= 0. Returns True if n is prime, and False otherwise. """ if n <= 1: return False if (n > 2) and (n%2 == 0): # Skip even numbers return False for d in range(3, int(math. sqrt(n)) + 1, 2): if n%d == 0: return False Why +1? return True Introduction to Programming © Dept. CS, UPC 8
Is there any real difference? Iterations Number of bits Introduction to Programming © Dept. CS, UPC 9
It makes a difference Introduction to Programming © Dept. CS, UPC 10
Prime factors • Write 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 2 • If divisible by d, divide and try again the same divisor • If not divisible, go to the next divisor – Keep dividing until the number becomes 1 Introduction to Programming © Dept. CS, UPC 11
Prime factors n 350 175 d 2 2 3 divisible yes no no 175 35 7 7 7 1 4 5 5 5 6 7 no yes no no yes finish write 2 5 5 7 The algorithm will never write a non-prime factor. Why ? Introduction to Programming © Dept. CS, UPC 12
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 13
Conclusions • Many 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
- Slides: 14