MATLAB Answers

How to determine if a number is prime?

339 ビュー (過去 30 日間)
Juan Zegarra
Juan Zegarra 2019 年 5 月 1 日
コメント済み: Rik 2019 年 5 月 2 日
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 件のコメント

Rik
Rik 2019 年 5 月 1 日
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).
Raj
Raj 2019 年 5 月 2 日

サインインしてコメントする。

採用された回答

jahanzaib ahmad
jahanzaib ahmad 2019 年 5 月 2 日
編集済み: jahanzaib ahmad 2019 年 5 月 2 日
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 件のコメント

Rik
Rik 2019 年 5 月 2 日
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).

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by