필터 지우기
필터 지우기

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