# Extracting gaps between prime terms in a sequence

I am trying to graph and extract the the 'gaps' between prime terms in the sequence:

a(1)=1

a(2)=2

for n>2, a(n)=2a(n-1) if a(n-1) is prime and a(n)=a(n-1)-1 if a(n-1) is not prime.

First of all, I can't even get the sequence to work. I am using:

 P=Primes();
for n in srange(3,1000):
if n in P:
print(2n)
elif n not in P:
print(n-1)


But I am getting a syntax error. I am not sure why.

Secondly, I would like to graph this sequence, but I cannot even get it to run.

Lastly, I really want to take this sequence and find the number of non-prime terms between two consecutive prime terms. I just want to print the length of the 'gap' between the prime entries in the sequence. For instance:

The sequence starts 1,2,4,3,6,5,10,9,8,7,14,13,26,25,24,23,46,45,44,43,86,85,84,83,166,165,164,163,326,...

So the first few 'gaps' would just be 1,1,3,1,3,3,3,3,9,3,3,15,...

Any help would be greatly appreciated!

edit retag close merge delete

Sort by » oldest newest most voted Here is some code

a = {1: 1, 2: 2}
for n in range(3, 20):
b = a[n - 1]
if ZZ(b).is_prime():
a[n] = 2 * b
else:
a[n] = b - 1
print(a)

more