How to find prime numbers between two inputs?

36 ビュー (過去 30 日間)
Yen Tien Yap
Yen Tien Yap 2021 年 4 月 25 日
回答済み: Pratheek Punchathody 2021 年 4 月 28 日
Hello,
How can I create a column vector with all prime numbers between two outputs? Eg. [10,100] and [200,20]. The two outputs can be in ascending or descending order. How can I code to satisfy these two conditions? Thank you.
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 25 日
hint: min(), max()

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

採用された回答

Pratheek Punchathody
Pratheek Punchathody 2021 年 4 月 28 日
We can use the "min()" and "max()" function to get the smallest and the largest numbers. "primes(n)" function is used to get all the prime numbers less than the 'n'.
Below code takes the input and displays all the prime numbers between the smallest and the largest input given. (irrespective of the input numbers in ascending or decending order).
x=[10,100]; %input
p=primes(max(x)); %calculate all the prime numbers less than min(x)
p1=primes(min(x)); %calculate all the prime numbers less then max(x)
y=setdiff(p,p1); % Set difference of two arrays
result = y'; %Converts the row vector to Column vector
Refer to the documentation on "setdiff()" for more information on difference of two arrays.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by