can someone tell me whats wrong with my code

i want to get the twin primes for the number 500 that is between 10 and 500 and the numbers has differnce by 2 like (17 19) i wrote this code but it seems there's somthing wrong and i can't get it
x = primes(500);
for i = 1:length(x)
while (x(i)>10) && (x(i)<500)
if (x(i+1) - x(i))= 2
disp (x)
end
end
end

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 25 Apr. 2016
Bearbeitet: Azzi Abdelmalek am 25 Apr. 2016

0 Stimmen

if (x(i+1) - x(i))= 2 is an error, use:
if x(i+1) - x(i)== 2
Also, use only the while loop
Image Analyst
Image Analyst am 25 Apr. 2016

0 Stimmen

Try it this way:
x = primes(500)
for i = 1:length(x)-1
if x(i) <= 10
continue;
end
if (x(i+1) - x(i)) == 2
fprintf('%d and %d\n', x(i), x(i+1));
end
end

1 Kommentar

Image Analyst
Image Analyst am 26 Apr. 2016
Note that I got rid of the while loop you had, otherwise you have an infinite loop.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 25 Apr. 2016

Kommentiert:

am 26 Apr. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by