while loops and prime numbers

hey guys, i am having trouble with a program that is supposed to keep generating a random number from 1 to 100 until it is prime. if its not then it will use a counter for the numbers that are not prime.
this is what I have so far, i just can't figure out what to put for the beginning next to the while and the if without it bugging.
any suggestions are helpful, thank you.
nInteger = 0;
random = randi(100);
while
if
nInteger = nInteger + 1;
end
end

댓글 수: 1

Dyuman Joshi
Dyuman Joshi 2023년 2월 15일
What is the purpose of the variable 'nInteger'? Is it to denote a prime number has been found?

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2023년 2월 14일

0 개 추천

Can't you use isprime()?
isprime(7)
ans = logical
1
isprime(4)
ans = logical
0
John D'Errico
John D'Errico 2023년 2월 15일

0 개 추천

First, create a list of all the primes that are less than 100. Save them. You can use either the function primes to do this, or just use a fast sieve to do that job.
Now generate random integers between 1 and 100. Test to see if they are in the list of primes. Stop when you find a prime.
Done.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2023년 2월 14일

댓글:

2023년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by