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

 

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

 

Molestiae et vero ipsa aut quod. Quod quasi maxime reprehenderit autem. Molestiae deleniti maiores quia et.

Sunt consequatur harum dignissimos hic unde suscipit. Optio omnis cumque non aut iusto quidem et doloribus. Nemo fugit qui consectetur aut necessitatibus eveniet quisquam.

Aut eius sit voluptas expedita. Et sed hic eum ut porro in.

Career Advancement Opportunities

March 2024 Investment Banking

  • Jefferies & Company 02 99.4%
  • Goldman Sachs 19 98.8%
  • Harris Williams & Co. (++) 98.3%
  • Lazard Freres 02 97.7%
  • JPMorgan Chase 03 97.1%

Overall Employee Satisfaction

March 2024 Investment Banking

  • Harris Williams & Co. 18 99.4%
  • JPMorgan Chase 10 98.8%
  • Lazard Freres 05 98.3%
  • Morgan Stanley 07 97.7%
  • William Blair 03 97.1%

Professional Growth Opportunities

March 2024 Investment Banking

  • Lazard Freres 01 99.4%
  • Jefferies & Company 02 98.8%
  • Goldman Sachs 17 98.3%
  • Moelis & Company 07 97.7%
  • JPMorgan Chase 05 97.1%

Total Avg Compensation

March 2024 Investment Banking

  • Director/MD (5) $648
  • Vice President (19) $385
  • Associates (86) $261
  • 3rd+ Year Analyst (13) $181
  • Intern/Summer Associate (33) $170
  • 2nd Year Analyst (66) $168
  • 1st Year Analyst (202) $159
  • Intern/Summer Analyst (144) $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
Betsy Massar's picture
Betsy Massar
99.0
4
BankonBanking's picture
BankonBanking
99.0
5
kanon's picture
kanon
98.9
6
CompBanker's picture
CompBanker
98.9
7
dosk17's picture
dosk17
98.9
8
DrApeman's picture
DrApeman
98.9
9
GameTheory's picture
GameTheory
98.9
10
bolo up's picture
bolo up
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...”