Recitation 2: Prime Numbers

advertisement
Discrete Mathematics for ECE
14:332:202 Spring 2004
Recitation 2: Prime Numbers
Introduction:
Prime numbers are an interesting topic in the study of Discrete Mathematics, and have very many significant
applications in the field of Cryptography. The Fundamental Theorem of Arithmetic simply stated says that
every natural number greater than 1 is either prime or made up of some combination of primes. To test a
number for being prime, we must first be familiar with the concept of divisibility.
To define Divisibility, we say that a divides b, notated as a | b, if there is some k such that a·k = b. For
example, 5 | 15 because 5·3 = 15. (i.e. a = 5, b = 15, k = 3). Thus if a does not divide b, then gcd(a,b) = 1.
According to the Fundamental Theorem of Arithmetic, prime numbers are not divisible by any other natural
numbers. We can test for divisibility by following the logical statement:
If a | b, then b mod a = 0
This is due to the fact that in modular arithmetic, integer multiples of the modular base are equal to zero. In
MATLAB, we would implement such a Boolean statement as something like:
(mod(b,a) == 0);
which would return a 1 if a | b. As you can expect, the way we check whether a number is n prime is to check
divisibility for all integers between 2 and floor(√n). As soon as some number 2 < a < √n divides n, we say that
n cannot be prime and the algorithm is ended.
The Sieve of Erastothanes is an efficient algorithm (also, a very old algorithm!), which will find all the primes
less than a given integer > 1. We could easily find all of these primes by a simple for loop, at each iteration
testing primeness, however this repeats many unnecessary calculations. The logic behind the Sieve is simple:
keep track and exclude all integer multiples of primes found in the main loop, because they cannot be prime.
This prevents many redundant calculations. The algorithm is as follows:
Mark all numbers as prime
For each number i = 2 to √n:
If i is prime
mark all multiples of i as not prime
Exercise:
1. Create a MATLAB function isitprime.m, which when given an input argument, n, will return a 1 if
n is prime, and 0 if n is not prime.
Note: your function declaration should read function isp = isitprime(n)
Example:
>> isitprime(33)
ans =
0
Hints:
a. See page 96 of Feil/Krone (it’s not MATLAB code).
2. Using your isitprime, create another function sieve.m which will implement the Sieve of
Erastothanes, and output into a vector of ones and zeros such that x(n) = 1 if n is prime.
Note: your function declaration should read function primes_vec = sieve(n)
Example:
>> sieve(5)
ans =
0
1
1
0
1
Hints:
See page 97 of Feil/Krone (again, not MATLAB)
b. Increment your index of the primes vector only if a number has been found to be prime:
c. Set primes_vec(1) = 0;
a.
Extra Credit:
Create a script M-file that will create a new figure and, using your Sieve function and the MATLAB plot
function, plot the calculation pi(x), which is defined as the number of primes less than or equal to x. Plot the
cumulative number of primes from 1 to 200, then in another figure from 1 to 20,000. Correctly title your figure
and axes. You do not have to create the function pi(x). Your figures should look like this:
(Hint: The total number of primes less than n (your y-axis) can be found as a cumulative sum,
cumsum(primes_vec)).
Matlab Reference:
MATLAB Variables: Variables in MATLAB can be given any name as long as it
1. Contains regular characters (alphabet, _, numeric)
2. Does not begin with a number.
Each variable by default can be used as a matrix, or for the one-dimensional case a
vector. Each element of the matrix can be accessed by subscripting in parenthesis.
example, if we wish to create a variable x, we can use it as a scalar
>> x = 4
x =
4
or, as a vector, which can be loaded element by element:
>> x(1) = 4
x =
4
>> x(2) = 8
x =
For
4
8
or loaded all at once:
>> x = [4 8 16]
x =
4
8
16
Two Dimensional matrices are subscripted by (row,column). For example:
>> y(1,1) = 1; y(1,2) = 2; y(2,1) = 3; y(2,2) = 4
y =
1
3
2
4
We also can load as a full matrix by separating the rows by semicolon:
>> y = [1 2; 3 4]
y =
1
3
2
4
>> z = y(1,2)
z =
2
Indexing
A range of integers is specified by the use of the colon.
specified by
J:K which is the same as [J, J+1, ..., K].
ex. 1:5 = [1 2 3 4 5]
Numbers in counting order are
Non-consecutive order
J:D:K which is the same as [J, J+D, ..., J+m*D] where m = fix((K-J)/D).
ex. 1:3:10 = [1 4 7 10]
Reverse order
J:-1:K
ex. 5:-1:1 = [5 4 3 2 1]
We use this notation in subscripting of variables, as we can access any range of elements
in a matrix. For example,
>> x(1:5) = 1
x =
1
1
1
1
1
Loops
The iterative nature of many operations leads us to program in loops.
such loop processes: for and while.
Matlab has two
A for loop allows us to repeat an operation a given number of times. A while loop will
repeat an operation until some condition is met. Both loops must be matched with an end
statement.
FOR
Syntax:
for variable = expression
statement;
end
Example: Say, we wish to print the squares of numbers 1 to 10, we could do this in
a for loop by writing:
for i = 1:10
disp(i^2);
end
WHILE
Syntax:
while (boolean expression)
statement;
end
Example:
n = 1;
while (n <= 10)
disp(n^2);
n = n+1;
end
Loops can also be nested, for example
for i = 1:5
for j = 1:5
x(i,j) = i*j;
end
end
IF
Syntax:
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
Vector Operations. We can often avoid the use of loops in MATLAB by taking advantage of
the vector notation. Whereas we use regular operators to work on scalar values (+*^, etc.), to achieve the operation on each element of a vector or matrix, we
precede the operation with a period.
For example, we could replace the following for loop:
for i = 1:10
x(i) = i^2
end
with
x(1:10) = (1:10).^2
PLOT
This is the simplest method by which to obtain MATLAB plots.
by which to plot.
There are several syntaxes
plot(Y) plots the values contained in the vector Y against their index
plot(X,Y) plots the vector Y versus the vector X. X and Y must be the same size.
To plot in a new figure, we type:
>> figure; plot( … );
For example, if we wish to plot the function y = x2, from 1 to 10, we would type as
follows:
>>
>>
>>
>>
>>
>>
x = 1:10;
y = x.^2;
figure; plot(x,y);
xlabel('x-axis');
ylabel('y-axis');
title('X^2')
The functions xlabel, ylabel, and title allow us to label our graph.
reference for more information on plotting.
See the MATLAB
Download