フィルターのクリア

is not prime number command , function to find the nearest prime number

2 ビュー (過去 30 日間)
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 7 月 27 日
コメント済み: Rik 2020 年 7 月 28 日
This is the code I have written :
function[k]=next_prime(n)
k=n
while ( k~=isprime(k))
k=k+1
if k~isprime(k)
end
end
I have tried to see what my error is by writing a similar function for the next nearest integer that divides by 5, and another function for the next nearest integer.
function[k]=mod5(n)
k=n
while ( mod(k,5)~=0)
k=k+1
end
end
The first function works as expected.
function[k]=nextint(n)
k=n
while ( k~=isinteger(k))
k=k+0.1
end
end
The second one, I seem to be having a similar issue as for the prime function ... it just keeps going adding 1 (0.1 respectively).
(I note that if i set k=1 and write the comman ' k~=isprime(k) ' it returns logical true , 1, whereas we would expect logical false? )
Many thanks for your help.

回答 (2 件)

Matt J
Matt J 2020 年 7 月 27 日
編集済み: Matt J 2020 年 7 月 27 日
function k=next_prime(n)
k=n
while ~isprime(k)
k=k+1
end
end

Prahelika Gayatri N
Prahelika Gayatri N 2020 年 7 月 28 日
function[k]=next_prime(n)
j=0;
while n>0
if j>n && isprime(j)
k=j;
return
end
j=j+1;
end
  1 件のコメント
Rik
Rik 2020 年 7 月 28 日
What does this answer add to that of Matt? Also, why didn't you format your code?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by