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

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 25 Avr 2016
Modifié(e) : Azzi Abdelmalek le 25 Avr 2016
if (x(i+1) - x(i))= 2 is an error, use:
if x(i+1) - x(i)== 2
Also, use only the while loop
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 commentaire

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 25 Avr 2016

Commenté :

le 26 Avr 2016

Community Treasure Hunt

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

Start Hunting!

Translated by