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!