MATLAB Answers

0

How to determine if a number is prime?

Asked by Juan Zegarra on 1 May 2019
Latest activity Commented on by Rik
on 2 May 2019
Hello, I was wondering if you can help how to determine if numbers from 0 to 100 are prime. Should I use loops? Please I am really confused with this homework.

  2 Comments

Rik
on 1 May 2019
There are many ways you could solve this. What was the exact assignment? I suspect you're not allowed to use the isprime function.
How would you solve this on paper? That's usually a good start for how to solve it in any programming language.
You can find guidelines for posting homework on this forum here (and there is also a lot of helpful advice on that page).

Sign in to comment.

1 Answer

Answer by jahanzaib ahmad on 2 May 2019
Edited by jahanzaib ahmad on 2 May 2019
 Accepted Answer

thats not difficult .try to solve it on paper first .for example u have a number 100 . how will u check that its prime or not ?
divide it with all numbers from 1 to 99 .. and if any time the remainder is zero its not a prime number
to divide 100 from 1 to 100 u can use for loop .

  1 Comment

Rik
on 2 May 2019
As is probably mentioned in the links posted above, you don't need to check up to 99, checking up to the square root of your number (and exiting the loop when you found a factor) will get you a big jump in performance.
An even better method would be to write a prime number sieve (use ismember to find the multiples).

Sign in to comment.