can someone tell me whats wrong with my code
Ältere Kommentare anzeigen
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
am 25 Apr. 2016
Bearbeitet: Azzi Abdelmalek
am 25 Apr. 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
1 Kommentar
tam FA
am 26 Apr. 2016
Image Analyst
am 25 Apr. 2016
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
am 26 Apr. 2016
Note that I got rid of the while loop you had, otherwise you have an infinite loop.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!