basic MATLAB question
Fellow Monkeys,
Shown below is a ridiculously simple MATLAB script that finds and displays the first N prime numbers. This is an example out of the Brandimarte book, with an attempted improvement by pre-allocating an empty matrix to the variable p. The variable "found" is an overall counter that keeps track of the iterations, ending the program when the Nth number is computed. The variable "trynumber" is the actual integer being used to find out if it is indeed a prime number. Listed below is the code and the output. I'm pretty sure my for/while lines might be out of order as the program can't get to the trynumber increment to go beyond two, hence 11 2's are displayed. What am I missing guys?
-Bryce
function p = myprimes(N) found = 0; trynumber = 2; p = zeros(1,N); while (found N) if isprime (trynumber); for i=1:N; p(i) = trynumber; found = found + 1; end trynumber = trynumber + 1; end end
myprimes(11) ans =
2 2 2 2 2 2 2 2 2 2 2
You shouldn't need the for loop...A quick look suggests this should work.
function p = myprimes(N) found = 0; trynumber = 2; p = zeros(1,N); while (found N) if isprime (trynumber); p(i) = trynumber; found = found + 1; end trynumber = trynumber + 1; end
should be p(found) not p(i)
function p = myprimes(N) found = 0; trynumber = 2; p = zeros(1,N); while (found N) if isprime (trynumber); p(found) = trynumber; found = found + 1; end trynumber = trynumber + 1; end
phdconsultant,
Thanks for the help. I'm still getting an error with the suggestions, but I tried something else and I got closer, except the output vector has these spacer zeros, but the correct number of prime numbers in the correct order. At this point I just need to get rid of those zeros in the output vector. This is what I have so far:
function p = myprimes(N) found = 0; trynumber = 2; p = zeros(1,N); while (found N) if isprime (trynumber); % for i=1:N; p(trynumber) = trynumber ; found = found + 1; end trynumber = trynumber + 1; end
myprimes(11) ans =
Columns 1 through 30:
0 2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0 0 0 23 0 0 0 0 0 29 0
Column 31:
31
inside your while loop, change
p(trynumber) = trynumber ; found = found + 1;
to
found = found + 1; p(found) = trynumber;
Quia rem explicabo ea nam velit aut. Consequatur eligendi ut autem autem dignissimos tempora. Est quia vitae magnam neque explicabo. Ullam et perspiciatis dicta non id quae quo laudantium.
Molestiae soluta vel voluptas doloribus occaecati. A maxime saepe ad odio. Pariatur inventore accusamus facere temporibus distinctio deleniti. Vel sunt tempora enim eum.
Dicta dolores qui quaerat atque cum ut. Modi et beatae qui aliquid officiis vero ducimus fuga. Ut aliquid facere qui consequatur.
Dolor atque voluptatibus maxime ratione minus possimus et et. Qui minima libero eos optio ut aliquid nam qui.
See All Comments - 100% Free
WSO depends on everyone being able to pitch in when they know something. Unlock with your email and get bonus: 6 financial modeling lessons free ($199 value)
or Unlock with your social account...