Find the amount of prime numbers between two numbers by using only length and primes

9 次查看(过去 30 天)
Is there any possible way to find the amount of prime numbers between two numbers by using only the commands length and primes? So far I have tried this, but whenever I change the number in the if statements to something larger than 2, the code suddenly won't work.
P = primes(20000);
p=[];
if P >= 10000
p = [p, P];
end
amount = length(p)

采纳的回答

Cris LaPierre
Cris LaPierre 2020-8-20
编辑:Cris LaPierre 2020-8-20
Yes, it is. Consider what primes returns: Prime numbers less than or equal to input value.
If I can find out the number of primes less than or equal to two different values, then I can use a little math to figure out the number of primes between those two values.
  7 个评论
Cris LaPierre
Cris LaPierre 2020-8-20
编辑:Cris LaPierre 2020-8-20
Yes. If I know there are 3 prime numbers <=6, and 8 prime numbers <= 20, then I can figure out the number of primes between 6 and 20 by taking the difference: 8-3 = 5.
iseinas
iseinas 2020-8-20
OMG it was really easy, I totally forgot about that,, thank you so much. My math skills really dropped right there :c You really helped a lot.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by