Smallest Prime Number Near to an Input of a Function

2 ビュー (過去 30 日間)
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
コメント済み: Ammar Taha 2019 年 6 月 27 日
I was doing Homework Assigment for a Course and Wrote small Function called "next_prime" which finds and returns k, the smallest prime number that is greater than n (Input).
It was Mandatory to Use while Loop and I know that this version is Full of Bugs but I Think It works well with Quite Small Values but with Very Large Ranges it Stopes so I need Youy Help to get a better Solution and here is the Function:-
function k = next_prime(n)
k_test = n+1;
while k_test > n
r = primes(n+15);
w = r(r>n);
k_test = w(1);
k = k_test;
return
end

回答 (1 件)

Tatvam Dadheech
Tatvam Dadheech 2019 年 6 月 27 日
編集済み: Tatvam Dadheech 2019 年 6 月 27 日
There is an inbuilt function to detect if a number is prime or not .i.e. isprime(n).
Try to loop over every number greater than n and check if its a prime or not using isprime function.
  1 件のコメント
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
I Tried what you 've said and it Worked Thanks :)

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

カテゴリ

Help Center および File ExchangeGPU Computing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by