Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
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.

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Oct. de 2017
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).']

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by