Twin primes between 10 en 500.

20 Ansichten (letzte 30 Tage)
Scooby
Scooby am 29 Nov. 2011
Kommentiert: Steven Lord am 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.

Antworten (3)

Andrei Bobrov
Andrei Bobrov am 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 am 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 am 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 Kommentare
waseem thasleem
waseem thasleem am 29 Nov. 2018
can u tell me where the documentation for matlab is ???~
~
Steven Lord
Steven Lord am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by