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

5 Comments
 

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

 

Ea dolores nemo voluptatem at eum. Aspernatur adipisci fuga tempora sint. Earum dolores commodi ut. Ipsam id accusantium corrupti quasi in.

Ut praesentium optio tenetur ut et placeat excepturi quidem. Ea harum aspernatur itaque numquam voluptatem. Fugit non officia maxime distinctio. Qui nostrum omnis voluptatibus odio libero qui suscipit atque. Vel hic neque dolorum eveniet illo quia.

Career Advancement Opportunities

June 2026 Investment Banking

  • Evercore 01 99.4%
  • Moelis & Company 01 98.8%
  • JPMorgan 01 98.2%
  • Guggenheim Partners 01 97.7%
  • Morgan Stanley 07 97.1%

Overall Employee Satisfaction

June 2026 Investment Banking

  • Moelis & Company No 99.4%
  • Morgan Stanley 01 98.8%
  • Evercore 01 98.2%
  • BMO Capital Markets 12 97.6%
  • Banco Santander 01 97.1%

Professional Growth Opportunities

June 2026 Investment Banking

  • Moelis & Company No 99.4%
  • Evercore No 98.8%
  • Morgan Stanley 05 98.2%
  • JPMorgan No 97.7%
  • BMO Capital Markets 12 97.1%

Total Avg Compensation

June 2026 Investment Banking

  • Vice President (14) $434
  • Associates (43) $259
  • 3rd+ Year Analyst (8) $210
  • 2nd Year Analyst (22) $179
  • Intern/Summer Associate (13) $156
  • 1st Year Analyst (75) $151
  • Intern/Summer Analyst (67) $101
notes
16 IB Interviews Notes

“... there’s no excuse to not take advantage of the resources out there available to you. Best value for your $ are the...”

Leaderboard

1
redever's picture
redever
99.2
2
Secyh62's picture
Secyh62
99.0
3
BankonBanking's picture
BankonBanking
99.0
4
kanon's picture
kanon
99.0
5
CompBanker's picture
CompBanker
98.9
6
Betsy Massar's picture
Betsy Massar
98.9
7
DrApeman's picture
DrApeman
98.9
8
dosk17's picture
dosk17
98.9
9
GameTheory's picture
GameTheory
98.9
10
Mimbs's picture
Mimbs
98.8
success
From 10 rejections to 1 dream investment banking internship

“... I believe it was the single biggest reason why I ended up with an offer...”