Project 0 In this project youll write a MATLAB program that

Project 0

In this project, you’ll write a MATLAB program that accepts a user-defined number, n, and find a list of all of the prime numbers that are greater than 1 and less than or equal to n.

For example, if I input 16 for n, your program should give me the following list:

2,3,5,7,11,13.

Part 1: Making a new MATLAB project

Start by opening the MATLAB program. Under the “Home” tab, click “New” and then click “Function.” The result should look something like this:

[page1image5608]

Replace the word “Untitled” with whatever you want to name your function; in this case, name it “primesUpTo”. The terms “output args” and “input args” are placeholders for whatever names you want to give to the outputs and inputs of your program. For this case, replace “output args” with “listOfPrimes,” since it’s going

1

[page1image8872] [page1image9032] [page1image9192]

to give you a list of prime numbers less than your input. Speaking of which, replace “input args” with “userDefinedNumber.” This will be what your program calls n.

The lines after the % signs are called comments. Whenever a line is preceded by the % symbol, the program ignores it. Comments exist only for humans to read, so that they can understand what your program does.

Somewhere between the line that begins with “function” and the line that says “end,” type the following line:

listOfPrimes = [];

This deserves some explanation. The symbol = has a different meaning for MAT- LAB than it does in mathematics. For MATLAB, = means “set.” In other words, this line of code is telling MATLAB to set the value known as listOfPrimes to [], which is the empty set.

Before you run the program, you’ll need to make sure that it’s saved properly. Select “Save As”, and then navigate to your S:\\drive. (This is the directory under “Computer” that is named after your UBITName.) It should look something like this:

[page2image9952]

2

[page3image384]

(Of course, “marksull” will be replaced with your own UBITName.) Navi- gate to Windows and save the file with its default name (which should be “prime- sUpTo.m”) in a new folder to hold your MATLAB projects.

With this done, you’ll need to tell MATLAB where it can find your file. Under the “Home” tab, select “Set Path”. Select “Add with subfolders,” and then navigate to the directory in which you saved “primesUpTo.m”. Select this, and then select Save. At this point, you might see the following message:

3

[page4image376]

Select “Yes”, and then navigate to the same folder in which you saved “prime- sUpTo.m”. Save there, and then select save again. Close the Set Path dialog.

Now, in the “command window,” type the following line:

primesUpTo(5)

Eventually, we’d like this command to actually give us the list of the prime numbers from 1 up to and including 5. For right now though, it’s just going to give us the empty set.

Part 2: Checking divisibility of a number

Well, now you have a program that accepts a user input, does nothing with it, and then gives you the empty set. Seems like there’s definitely some room for im- provement there. The first step in finding the list of primes from 1 to n, however, is

4

being able to recognize whether a number is prime or not.
A positive integer a is prime if the only positive integers that divide it are a and

1. An integer b divides a if and only if the remainder when a is divided by b is 0. MATLAB has a built-in function that gives the remainder. It’s called mod. Given any integers a and b, if you type mod(a, b) into the command window, MATLAB will return the remainder when a is divided by b. For example, typing mod(99, 4) will give 3, typing mod(17, 3) will give 2, and typing mod(1027, 50) will give 27.

Now, in order to determine whether a divides b, you’ll need to know whether mod(a, b) is 0 or not. If it is, then a is divisible by b. If it isn’t, then a is not divisble by b.

Part 3: Checking whether a number is prime or not

In order to determine whether a number a n is prime or not, you’ll need to check all of the remainders of a divided by x, for all of the values of x such that 1<x<a. Butallweknowaboutaisthatan;howcouldwepossiblyknow what values of x to check? We don’t, but the program will.

Let’s say we want to list the primes from 1 to 6.

We’d want the program to first look for integers that divide 2. Then, we’d want the program to look for integers that divide 3. Then, we’d want the program to look for integers that divide 4. Then, we’d want the program to look for integers that divide 5. Then, we’d want the program to look for integers that divide 6.

5

The pattern here is very simple: just do something several times, where the only thing that changes between the iterations is one number. This can be accomplished with what’s called a for loop.

Type the following into your program.

[page6image2792]

This defines a value called “currentNumber” as 5, and then executes the code be- tween for and end for each value x such that 2 x currentNumber 1. The code currently just displays the phrase “MY NEW FAVORITE NUMBER IS:” followed by a number between 1 and currentNumber, which is referred to as possibleDivisor. If you run your code now, the phrase will pop up three times on your screen. Likewise, if you replace 5 with any other greater than or equal to 2, the phrase will pop up as many times as there are numbers between 2 and currentNumber 1. In the same manner, if you modify the code to say the follow- ing:

[page6image8960]

6

and then run the code, then the code will display the phrase “MY NEW FAVORITE NUMBER IS:” and then each of the numbers from 2 up to 1 less than the input.

We can use this same technique to check every number x from 1 up to a num- ber to see whether that number is divisible by x. In order to make the program discriminate between when it is divisible and when it’s not, we’ll need to use an if statement, which executes a block of code only when the condition proceeding it is true:

Now, if you run the code, the function will tell you if the number you’ve en- tered is not prime. For example, entering primesUpTo(12) will cause the program to say “This number is not prime” (several times). On the other hand, entering primesUpTo(41) will not trigger this statement, indicating that 41 actually is prime.

Notice the == in the if statement. This is different from the =, which means to assign a value. The == means “check if these are equal;” when MATLAB reads this code, it determines whether mod(userDefinedNumber,possibleDivisor) re- ally is equal to 0. If so, it executes the code that follows, until it reaches the end statement. If not, it simply skips over it.

7

[page7image11456]

Part 4: What if the number is prime?

Right now, our program has listOfPrimes as the empty set. In order to make listOfPrimes give us a list of prime numbers, we simply need to add some elements to it. In MATLAB, this is very simple. Let’s say that you’re looking to add the numbers 2, 3 and 5 to listOfPrimes, in order. To do this, you only need to index listOfPrimes and then assign a value to that position, like so:

listOfPrimes(1) = 2; listOfPrimes(2) = 3; listOfPrimes(3) = 5;

Now listOfPrimes is the array [2, 3, 5]. To add 7, assign listOfPrimes(4) = 7, so that listOfPrimes becomes the array [2, 3, 5, 7].

On the other hand, if you have an array, like R = [4, 5, 2], and you’d like to re- place that 2 with 4, then the command is R(3) = 4, and the array becomes [4, 5, 4].

Part 5: Now you’re on your own

The rest of this problem will depend mainly on things that have been discussed here. Right now the program determines whether the input is prime, but what we want is a list of the primes less than or equal to the input. Therefore, here’s what your program still needs to do:

1. Check each positive integer less than or equal to the input to determine whether it is prime or not.

8

2. If it is prime, put it in listOfPrimes. If it is not prime, then discard it.

It may be helpful at first to deal with concrete numbers before dealing with user- defined values. For example, try designing the program to find the primes between 1 and 20, and then modifying it so that it works for any integer.

The disp command is helpful for determining what’s going on while the pro- gram is running. For example, if your program doesn’t work because it isn’t ex- ecuting the code inside of an if statement correctly, try putting disp(“If statement triggered”) inside of the if statement and looking at the output. Now, if you never see the phrase “If statement triggered,” then you know that, for some reason or another, the condition of your if statement was never true during the program’s execution.

9

Solution

MatLab Code:

function [listofPrimes]=primesUpTo(UserDefinedNumber)
k=1;
for i=1:UserDefinedNumber
factors_count=0;
for j=1:i
if mod(i,j)==0
factors_count=factors_count+1;
end
end
if factors_count==2
listofPrimes(k)=i;
k=k+1;
end
end
end

OUTPUT:
>> primesUpTo(30)
ans =
2 3 5 7 11 13 17 19 23 29
>> primesUpTo(100)
ans =
Columns 1 through 15
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Columns 16 through 25
53 59 61 67 71 73 79 83 89 97

Project 0 In this project, you’ll write a MATLAB program that accepts a user-defined number, n, and find a list of all of the prime numbers that are greater tha
Project 0 In this project, you’ll write a MATLAB program that accepts a user-defined number, n, and find a list of all of the prime numbers that are greater tha
Project 0 In this project, you’ll write a MATLAB program that accepts a user-defined number, n, and find a list of all of the prime numbers that are greater tha
Project 0 In this project, you’ll write a MATLAB program that accepts a user-defined number, n, and find a list of all of the prime numbers that are greater tha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site