How do I find the nearest prime number greater than the input? This is what I've managed to do so far...
조회 수: 3 (최근 30일)
이전 댓글 표시
function out = next_prime(X)
out = 0;
n = 0;
while out == isprime(X)
n = n + 1;
out = out + n + X;
end
end
댓글 수: 0
답변 (1개)
Deepak
2023년 7월 1일
You can complete your code with this method -
function out = next_prime(X)
out = X + 1; % Initialize the output as the next number after X
while ~isprime(out)
out = out + 1; % Increment the output by 1 until a prime number is found
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!