I'm in my first year studying for my BSc in maths and I have the following problem to solve by Sunday: Write a Maple procedure with the entry of a real number x that will count the number of twin primes (p,p+2) with p+2<=x. Plot the results in a procedure between 1 and 1 000 000 I have the procedure to find the number of twin primes, but I can't seem to put it into a graph. Here's my progress:
primzahl:=proc(n)
local a,Q;
Q:=0;
for a from 2 to n-2 do
if isprime(a) then
if isprime(a+2) then
Q:=Q+1;
fi;
fi;
od;
end:
Thanks in advance,
emj
Your routine calls
isprimeon everyabetween2andn-2. It could be done faster by usingnextprimeto get to the next candidateato test.Also, it's much better to have the
primzahlprocedure keep a running total, so that it can store the values where new twin primes are found. It looked like you intended on calling your original procedure many times, to figure out the totals separately. That'd be unnecessarily costly.[edited] And another minor adjustment skips the
nextprimecall in the case that a twin prime pair was found (as the next candidateacan just be taken asa+2).