I need a function that returns the smallest prime number p smaller than 100,000 such that (p + n) is also prime, where n is a scalar integer and is the sole input argument to the function

1 次查看(过去 30 天)
Write a function called prime_pairs that returns the smallest prime number p smaller than 100,000 such that (p + n) is also prime, where n is a scalar integer and is the sole input argument to the function. If no such number exists, then the function returns -1. You may use the built-in functions, primes and isprime.
  3 个评论

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-8-27
编辑:Stephen23 2015-8-27
function p = prime_pairs(n)
V = primes(1e5);
V = V(isprime(n+V));
if isempty(V)
p = -1;
else
p = V(1);
end
end

更多回答(1 个)

charu sharma
charu sharma 2015-8-27
For simple code refer this and ask if you have any query: http://farzicoders.blogspot.in/2015/08/write-function-called-primepairs-that.html
  1 个评论
Stephen23
Stephen23 2015-8-27
"simple code" with:
  • poorly named variables: for i=1:l
  • superfluous intermediate variables: flag
  • superfluous loops when code vectorization would be neater.
  • superfluous return statements.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by