# Matlab code to find all prime numbers between 2 and 5000

Discussion in 'Programmer's Corner' started by Dapnc, Feb 7, 2013.

1. ### Dapnc Thread Starter New Member

Feb 7, 2013
3
0
Need to write a matlab program that will find all prime numbers between 2 and 5000 but I can't use any built in matlab functions. Please help. It also has to run quickly.

2. ### tshuck Well-Known Member

Oct 18, 2012
3,531
675
There are plenty of algorithms out there, what prevents you from using one of those? Or, are you wanting us to write the MATLAB code for you?

How quickly is quickly?

3. ### Dapnc Thread Starter New Member

Feb 7, 2013
3
0
Having trouble not using matlab built-in functions. Just need help getting started.

Oct 18, 2012
3,531
675
5. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
num = prime(5000)

Using that internal function, write the rest of the program, such as desired output format, optional input value for number (just like the built in function).

Once that is complete, Write a prime number algorithm. Run both prime(5000) and yourfunc(5000) to compare time to run, (remember to compile for max speed if you didn't know that). Once you get an algorithm that is close or less than the builtin function AND produces the same results, you are done.

That, combined with the algorithm link above should be all you need to get you started conceptually.

6. ### vortmax Member

Oct 10, 2012
103
18
another good trick with matlab: many builtin functions are written in m-code, so you can see how they work by running `edit <fcn>`. However, the low level functions tend to be written in C, and so you can't see how they do their magic.

7. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
Download a list of primes <=5000 from the internet, then install it in Matlab as a lookup table.

That is very easy to get a finished result and will really "run quickly".

djsfantasi, jaygatsby and thatoneguy like this.
8. ### tgstanfi New Member

Feb 22, 2013
1
0
Hi! I am having to do this same project and I was just wondering if you ever figured out a code that worked? I have been working on it for hours now and I started one but it ran forever and isn't right. I would greatly appreciate it!

9. ### tshuck Well-Known Member

Oct 18, 2012
3,531
675
This site is not meant for sharing homework answers, it is, however, for educational purposes. If you'd like to start your own thread and post what you came up with, we'd be glad to take a look at it for you...

Jan 8, 2013
2
0