Download Definition

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1/27/2015
Agenda
Numerical Algorithms
•
•
•
•
•
•
Randomizing Data
Greatest Common Divisors
Exponentiation
Prime Numbers
Summary
Exercises
Definition
Prime Numbers
• Finding prime numbers
• Sieve of Eratosthenes
• Prime Number – a number that has only two factors, itself and 1.
E.g., 7 is prime because the only numbers
that will divide into it evenly are 1 and 7.
Primality Testing
// Return true if the number n is prime.
Boolean: IsPrime1(Integer: n)
// Pre: n > 4
For j = 2 to sqrt(n)
if (n mod j == 0) return false
next j
// The number n is prime.
Return true
End IsPrime1
Primality Testing
// Return true if the number n is prime.
Boolean: IsPrime2(Integer: n)
// Pre: n > 4
if (n mod 2 == 0) return false
For j = 3 to sqrt(n)
if (n mod j == 0) return false
next j += 2
// The number n is prime.
Return true
End IsPrime2
O(sqrt(n))
O(sqrt(n))
1
1/27/2015
Eratosthenes
(ehr‐uh‐TAHS‐thuh‐neez)
• Eratosthenes was a Greek mathematician, astronomer, and geographer.
• He invented a method for finding prime numbers that is still used today.
• This method is called Eratosthenes’ Sieve.
Eratosthenes’ Sieve
• A sieve has holes in it and is used to filter out the juice.
• Eratosthenes’s sieve filters out numbers to find the prime numbers.
Hundreds Chart
Hundreds Chart
• On graph paper, make a chart of the numbers from 1 to 100, with 10 numbers in each row.
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
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
10
20
30
40
50
60
70
80
90
100
1 – Cross out 1; it is not prime.
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
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
10
20
30
40
50
60
70
80
90
Hint For Next Step
• Remember all numbers divisible by 2 are even numbers.
100
2
1/27/2015
2 – Leave 2; cross out multiples of 2
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
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
10
20
30
40
50
60
70
80
90
100
Hint For Next Step
• To find multiples of 3, add the digits of a number; see if you can divide this number evenly by 3; then the number is a multiple of 3. 267
Total of digits 2+6+7= 15
3 divides 15, so does 267.
That is, a number is a multiple of 3
iff the sum of its digits is a multiple of
3.
3– Leave 3; cross out multiples of 3
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
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
10
20
30
40
50
60
70
80
90
100
4– Leave 5; cross out multiples of 5
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
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
10
20
30
40
50
60
70
80
90
100
Hint For the Next Step
• To find the multiples of 5 look for numbers that end with the digit 0 and 5.
385 is a multiple of 5
& 890 is a multiple of 5
because the last digit
ends with 0 or 5.
5– Leave 7; cross out multiples of 7
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
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
10
20
30
40
50
60
70
80
90
100
3
1/27/2015
6–Leave 11; cross out multiples of 11
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
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
10
20
30
40
50
60
70
80
90
100
The Prime Numbers from 1 to 100 are as follows:
2,3,5,7,11,13,17,19,
23,31,37,41,43,47,
53,59,61,67,71,73,
79,83,89,97
Primality Testing
All the numbers left are prime
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
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
10
20
30
40
50
60
70
80
90
100
Primes Finding
// Return all the primes no bigger than n.
CollectPrime(Integer: n)
A[0] = 2; A[1] = 3; A[2] = 5; A[3] = 7;
s = 4;
for p = 9 to n
for j = 1 to s-1
if (A[j] > sqrt(p)) goto L1
if (p mod A[j] == 0) goto L2
next j
L1:
A[s++] = p
L2: next p += 2
// A contains s primes.
End CollectPrime
Two Types of Randomized algorithms
// Return true if the number p is (probably) prime.
Boolean: IsPrime(Integer: p, Integer: max_tests)
// Perform the test up to max_tests times.
For test = 1 To max_tests
<Pick a random number n in [1..p-1]>
If (np-1 Mod p != 1) Then Return false
Next test
// The number is probably prime.
// (There is a 1/2max_tests chance that it is
not prime.)
Return true
End IsPrime
• Las Vegas
• Monte Carlo
Runtime: O(max_tests*log(p))
IsPrime is a Monte Carlo algorithm.
4
1/27/2015
Las Vegas
•
•
•
•
Always gives the true answer.
Running time is random.
Running time is bounded.
Quicksort is a Las Vegas algorithm.
Monte Carlo
• It may produce incorrect answer!
• We are able to bound its probability.
• By running it many times on independent random variables, we can make the failure probability arbitrarily small at the expense of running time.
• IsPrime is a Monte Carlo algorithm.
Monte Carlo Example
Monte Carlo Example
• Suppose we want to find a number among n given numbers which is larger than or equal to the median.
Suppose A1 < … < An which are randomly stored in a file F.
We want Ai, such that i ≥ n/2.
It’s obvious that the best deterministic algorithm needs O(n) time to produce the answer.
n may be very large! Suppose n is 100,000,000,000 !
Monte Carlo Example
• Choose 100 of the numbers in F with equal probability. • find the maximum among these numbers.
• Return the maximum.
Monte Carlo Example
• The running time of the given algorithm is O(1).
• The probability of Failure is 1/(2100).
• Consider that the algorithm may return a wrong answer but the probability is smaller than the hardware failure or even an earthquake!
5