I am currently trying to write a code that spits out all the prime numbers less than an input, n, that also have a difference of 2. So, 3 and 5, 5 and 7, and 17 and 19 and I also need to put them into a 2 column matrix. This is what I have so far.

2 ビュー (過去 30 日間)
tableName = zeros(1,2);
n = input('Integer');
a = primes(n)
I get a vector of all the prime numbers less than or equal to my input n, but after that I'm not sure how to only single out and keep the ones that have a difference of two.

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 6 日
Hint:
t = randi(6,1,100);
which = find(t(1:end-2) == t(2:end-1)+t(3:end));
[t(which).', t(which+1).', t(which+2).']

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by