can someone tell me whats wrong with my code
Show older comments
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)
Azzi Abdelmalek
on 25 Apr 2016
Edited: Azzi Abdelmalek
on 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
Image Analyst
on 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 Comment
Image Analyst
on 26 Apr 2016
Note that I got rid of the while loop you had, otherwise you have an infinite loop.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!