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
Show older comments
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
MrFish
on 30 Apr 2014
John D'Errico
on 30 Apr 2014
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?
John D'Errico
on 30 Apr 2014
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?"
John D'Errico
on 30 Apr 2014
Edited: John D'Errico
on 30 Apr 2014
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?"
Roger Stafford
on 30 Apr 2014
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.
Answers (1)
Roger Stafford
on 30 Apr 2014
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
MrFish
on 5 May 2014
Roger Stafford
on 5 May 2014
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'?
MrFish
on 7 May 2014
MrFish
on 8 May 2014
Walter Roberson
on 2 Dec 2019
Edited: Walter Roberson
on 2 Dec 2019
disp(array(array>0));
Reminder: using ones as the name of a variable is likely to lead to problems in the future.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!