Trying to do a Sieve of Eratosthenes question. It's working to an extent where the numbers appear correctly but adds a number after the previous number. For exmaple: 2 then 2, 3, then 2, 3, 5 then 2,3,5,7

n = 101;
array = 2:n;
ones = [];
for i = 1:length(array)
if ~ array(i) == 0
for j = i+1: length(array)
if rem(array(j), array(i)) == 0
array(j) = 0;
end
end
end
if ~ array(i) == 0
ones(length(ones) + 1) = array(i)
end
end

5 Comments

Keep in mind that I can't use many fucntions such as zeros(), disp(). If you spot an issue with my fucntions, please advise. Thanks
I've edited your post to show the formatting correctly, but please do this yourself when you make a post. If you specify that a block of code is code, then see how nice it looks? Now it is readable. Before?
And, oh, by the way, never use the name ones as a variable, or sum, zeros, etc. You WILL be sorry you did, or your next question here will be "Why does the function ones no longer work for me?"
One more thing. It is very inefficient in MATLAB to build arrays as you have done with the ones array, incrementally. This forces MATLAB to reallocate memory for that array at every step. If that array gets large, again, you will be sorry. Your next question here would then be, "Why is my code so slow?" Or "How can I make my code faster?"
He does have the problem of supposedly not knowing in advance how many primes would be found and therefore how long the array should be.
Also he has said he couldn't use functions like 'zeros', so there might be some difficulty in allocating his array in advance. It must be frustrating for students have their hands "tied" in such a manner for homework assignments.

Sign in to comment.

Answers (1)

Because you have not ended the line
ones(length(ones) + 1) = array(i)
with a semicolon, it is displaying at each step the entire contents of the 'ones' array as it has developed up to that point. That is why you see at the first step 2, then at the next step 2,3, then 2,3,5, etc. It does not mean that, for example, the 2 has been added more than once to 'ones'. When this code is finished, 'ones' will contain just one of each prime up to 101, just as you intended. To avoid this confusion, put a semicolon after this line and only display the contents of 'ones' at the end of the code.
By the way, it is not a good policy to use the string 'ones' for the name of an array, since that is a reserved word in matlab, though that has nothing to do with the display problem you have.

5 Comments

I added the semicolon - obviously this suppresses the process up until the last line. That's obviously what I am aiming to do except, it still doesnt work.
This time I am getting the name of the file. What do you think I should do now?
Could you please explain in greater detail in what way "it still doesn't work". Show at least part of the results you are getting in 'ones' that are not correct. Exactly what do you mean by "getting the name of the file"? Getting it where? Also have you taken our advice and changed the name from 'ones'?
I am receiving the name of my project. As in, my MATLAB file is called Matlabprimeassignment.m so when I compute the code, it comes up: >> MATLAB prime assignment.
Also, I can't change the name of the 'ones' because as I mentioned before I can't use anything else besides ones, fix and disp. I thought I could use zeros but my teacher said that its unacceptable.
I'll comment in a short bit with my program's answers. And I'll show you what I mean by getting my file name.
%%This is the Primes Question
clear;
n = 101;
array = 2:n;
ones = [];
for i = 1:length(array)
if ~ array(i) == 0
for j = i+1: length(array)
if rem(array(j), array(i)) == 0
array(j) = 0;
end
end
end
if ~ array(i) == 0
ones(length(ones) + 1) = array(i);
end
end
And this is what comes up in command window: >> matlab_prime_assignment
disp(array(array>0));
Reminder: using ones as the name of a variable is likely to lead to problems in the future.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 30 Apr 2014

Edited:

on 2 Dec 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!