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.
0 件のコメント
回答 (1 件)
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).']
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!