PDA

View Full Version : MATLAB code to display prime numbers?

szak1592
12-06-2011, 03:40 PM
Can you guys please help me with a MATLAB code, I need a simple program to display the prime numbers between 1 and 100. Please help as I am new to programming and I have to do it as an assignment.

alykins
12-06-2011, 06:19 PM
it has been a while since I have done MATLAB (and I did not do a whole lot in it but I will help you with the logic flow so you can figure it out) You cannot post questions on "how do I do this" for assignments, but you can ask for help on why things are not working... this pseudo code may help you

int[] myNums = new int[100]
i=1
totalprime =0
while (i<=100){
bool eval = true
cnt = 0
while(eval = true){
for(int y=1; y<=i; y++)
if(i%y != 0) eval = false
else cnt++
if (y==i) eval false
}
if(cnt == 2) myNums[totalprime] = i
i++
}

i think that logic flow is correct... take a look at it and tell me if you can figure out what it is doing... a key to doing this is going to be modulus