Ask Your Question
1

Extracting gaps between prime terms in a sequence

asked 2020-10-29 14:20:44 +0200

brennan gravatar image

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 flag offensive close merge delete

Comments

2n is not correct, you shoud write 2*n

FrédéricC gravatar imageFrédéricC ( 2020-10-29 14:49:55 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-10-29 14:50:08 +0200

FrédéricC gravatar image

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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-10-29 14:20:44 +0200

Seen: 123 times

Last updated: Oct 29 '20