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.

1 次查看(过去 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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by