How to write a function that will count the number of prime numbers betweeen n1 and n2 using a for loop?

38 ビュー (過去 30 日間)
I need to write a function CountPrimes(n1, n2) that will count the number of prime numbers between n1 and n2 and return the count as the output argument. I need to implement this using a for loop.
I was trying to use isprime(x) to check whether x is a prime number
An example of how it should run is,
>> num=CountPrimes(10, 20)
num = 4
  2 件のコメント
John D'Errico
John D'Errico 2018 年 9 月 23 日
編集済み: John D'Errico 2018 年 9 月 23 日
So why not try it? You know what you want to do. A loop that runs between the two limits. Surely you know what a loop is. You already know what isprime is and what it does. Surely you can add 1 to a counter. Test each number to see if it is prime. Then just use a counter, adding 1 to the counter every time you find a prime. This will work in theory.
So what is the problem? What question are you asking? (If your question is what code do you need to write to do your assignment, that is the wrong question, and it is not even a question about MATLAB, just a doit4me.) So make an effort. When you get stuck, THEN ask a real question.
Note that what you are planning to do will be a bit inefficient if the limits are very large and far apart. So a billion calls to isprime will take some time.
Samantha Farmer
Samantha Farmer 2018 年 9 月 23 日
編集済み: Stephen23 2018 年 9 月 24 日
Yes, I should've put what I was having trouble with. So what I have so far is:
function CountPrimes(n1,n2)
for x = n1:n2 %x is numbers in between num1 and num2
y = isprime(x); %y is to find the primes between the 2 numbers
end
fprintf('The primes between %d and %d are: \n',n1, n2); %tells the user the output
disp(y); %display the primes
end
It keeps saying that the number of primes is zero or when I run num=CountPrimes(10,20) that there are too many output arguments. Do I need to define n1, and n2? Or just have them as input arguments like n1-input('enter a number') ?

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

回答 (3 件)

John D'Errico
John D'Errico 2018 年 9 月 24 日
編集済み: John D'Errico 2018 年 9 月 24 日
Your problem is you needed to do something given the result of isprime(x). You stuffed that result into y. But then you did nothing based on that test. So you had the beginnings of an idea that would work, at least to start.
isprime(x) returns 0 (false) if the number is not prime. It returns 1 (true) if the number is prime.
Do you want to merely count the primes, or do you also want to display them?
Anyway, based on what you wrote, this adds the mechanism of using the result of isprime.
function CountPrimes(n1,n2)
Counter = 0; % initialize to zero.
for x = n1:n2 %x is numbers in between num1 and num2
y = isprime(x); %y is true if x is prime
if y % did we find a prime at this value of x?
Counter = Counter + 1; % increment the count of primes
disp([num2str(x),' is a prime'])
end
end
disp(['The total number of primes found was: ',num2str(Counter)])
end

Walter Roberson
Walter Roberson 2018 年 9 月 23 日
for K = n1 : n2
test to see if K is prime
if it was prime
num = num + 1;
end
end

Andrei Bobrov
Andrei Bobrov 2018 年 9 月 24 日
Without loops
CountPrimes = @(n1,n2)sum(isprime(n1:n2));
use
>> CountPrimes = @(n1,n2)sum(isprime(n1:n2));
>> CountPrimes(10,20)
ans =
4
>>

カテゴリ

Help Center および File ExchangeDiscrete Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by