필터 지우기
필터 지우기

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 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