Twin primes between 10 en 500.

4 views (last 30 days)
Scooby
Scooby on 29 Nov 2011
Commented: Steven Lord on 29 Nov 2018
Hi.
I have to find all twin primes (pairs of prime numbers such that the difference between them is 2, for example 17 en 19) between 10 and 500. I can use primes(500) but how can I find all the prime numbers larger than 10 (and smaller than 500)?
Let's say x are all the prime numbers smaller than 500. Then with the function find(x>10) I can find the positions of the numbers but how do I let Matlab display them?
And to find the couples, can I use something like if rem(x/2)==0? And then also let matlab display them? (How?!)
I don't if I have to you disp of fprintf or something like that, so please help.
Thank you.

Answers (3)

Andrei Bobrov
Andrei Bobrov on 29 Nov 2011
a = primes(500);
a = a(a>10);
n = find(diff([a(1:end-1);a(2:end)])==2);
out = a([n;n+1]).'

Daniel Shub
Daniel Shub on 29 Nov 2011
This is pretty basic MATLAB. You need to read the getting start part of the manual. To display the primes greater than 10
x = primes(500); % x is a list of the primes less than 500
k = find(x > 10)
x(k)
y = x(k); % y is a list of the primes greater than 10 and less than 500
The command rem(x/2) is not valid you might mean rem(x, 2), but I have no idea how this would help you determine if the difference between two primes is 2. I would suggest
doc diff

Titus Edelhofer
Titus Edelhofer on 29 Nov 2011
Hi Scooby,
I guess your questions are not directly related to prime numbers but to learning MATLAB ... ;-). I'd suggest to read the "Getting Started" chapter in the documentation to learn the very basics and then try this assignment ...
Titus
  2 Comments
waseem thasleem
waseem thasleem on 29 Nov 2018
can u tell me where the documentation for matlab is ???~
~
Steven Lord
Steven Lord on 29 Nov 2018
If you have MATLAB installed, type this command in the MATLAB Command Window:
doc
If you don't have MATLAB installed, the documentation is available in the Support section of the MathWorks website.
Another resource that may help you learn MATLAB, particularly if you prefer learning by watching videos and working exercises than reading, is the Tutorials item also on the Support section of the website.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!