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

Answers (2)

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 Comment

Note that I got rid of the while loop you had, otherwise you have an infinite loop.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 25 Apr 2016

Commented:

on 26 Apr 2016

Community Treasure Hunt

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

Start Hunting!