ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 29 Oct 2020 14:50:08 +0100Extracting gaps between prime terms in a sequencehttps://ask.sagemath.org/question/54073/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!Thu, 29 Oct 2020 14:20:44 +0100https://ask.sagemath.org/question/54073/extracting-gaps-between-prime-terms-in-a-sequence/Comment by FrédéricC for <p>I am trying to graph and extract the the 'gaps' between prime terms in the sequence: </p>
<p>a(1)=1</p>
<p>a(2)=2</p>
<p>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. </p>
<p>First of all, I can't even get the sequence to work. I am using:</p>
<pre><code> P=Primes();
for n in srange(3,1000):
if n in P:
print(2n)
elif n not in P:
print(n-1)
</code></pre>
<p>But I am getting a syntax error. I am not sure why. </p>
<p>Secondly, I would like to graph this sequence, but I cannot even get it to run.</p>
<p>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:</p>
<p>The sequence starts 1,<strong>2</strong>,4,<strong>3</strong>,6,<strong>5</strong>,10,9,8,<strong>7</strong>,14,<strong>13</strong>,26,25,24,<strong>23</strong>,46,45,44,<strong>43</strong>,86,85,84,<strong>83</strong>,166,165,164,<strong>163</strong>,326,...</p>
<p>So the first few 'gaps' would just be 1,1,3,1,3,3,3,3,9,3,3,15,...</p>
<p>Any help would be greatly appreciated!</p>
https://ask.sagemath.org/question/54073/extracting-gaps-between-prime-terms-in-a-sequence/?comment=54074#post-id-540742n is not correct, you shoud write 2*nThu, 29 Oct 2020 14:49:55 +0100https://ask.sagemath.org/question/54073/extracting-gaps-between-prime-terms-in-a-sequence/?comment=54074#post-id-54074Answer by FrédéricC for <p>I am trying to graph and extract the the 'gaps' between prime terms in the sequence: </p>
<p>a(1)=1</p>
<p>a(2)=2</p>
<p>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. </p>
<p>First of all, I can't even get the sequence to work. I am using:</p>
<pre><code> P=Primes();
for n in srange(3,1000):
if n in P:
print(2n)
elif n not in P:
print(n-1)
</code></pre>
<p>But I am getting a syntax error. I am not sure why. </p>
<p>Secondly, I would like to graph this sequence, but I cannot even get it to run.</p>
<p>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:</p>
<p>The sequence starts 1,<strong>2</strong>,4,<strong>3</strong>,6,<strong>5</strong>,10,9,8,<strong>7</strong>,14,<strong>13</strong>,26,25,24,<strong>23</strong>,46,45,44,<strong>43</strong>,86,85,84,<strong>83</strong>,166,165,164,<strong>163</strong>,326,...</p>
<p>So the first few 'gaps' would just be 1,1,3,1,3,3,3,3,9,3,3,15,...</p>
<p>Any help would be greatly appreciated!</p>
https://ask.sagemath.org/question/54073/extracting-gaps-between-prime-terms-in-a-sequence/?answer=54075#post-id-54075Here 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)
Thu, 29 Oct 2020 14:50:08 +0100https://ask.sagemath.org/question/54073/extracting-gaps-between-prime-terms-in-a-sequence/?answer=54075#post-id-54075