Susan
Susan

Reputation: 301

Python, generate prime twins

I tried to write a python program to generate the prime twins in sage.

However I can't quite seem to get it to work.

Code: http://pastebin.com/PRnvJsf3

Sage is giving me the following error message:

Traceback (most recent call last):    y = 1
  File "", line 1, in <module>

  File "/private/var/folders/6-/6-7wRVdAEhuqw8H28uS60U+++TI/-Tmp-/tmp8b0dCT/___code___.py", line 19
    while s1[y] < s1[len(s1)]
                            ^
SyntaxError: invalid syntax

Thanks i put in the missing colons.

Now i get

Traceback (most recent call last): y = 1 File "", line 1, in

File "/private/var/folders/6-/6-7wRVdAEhuqw8H28uS60U+++TI/-Tmp-/tmpEIEhQs/code.py", line 19 if (s1[k] + s1[y]) == ((s1[k] + (s1[k] + _sage_const_2 )): ^ SyntaxError: invalid syntax

Thanks, i also put in the missing parentheses

now i get

Traceback (most recent call last): y = 1 File "", line 1, in

File "/private/var/folders/6-/6-7wRVdAEhuqw8H28uS60U+++TI/-Tmp-/tmp9RE23a/code.py", line 18, in while s1[y] < s1[len(s1)]: IndexError: list index out of range

Hmm, i suppose i cannot use len(s1) here.

Thanks so much, it is working now

while s1[y] < s1[len(s1) - 1]:

However,

s2 = s2 + s1[k] does not seem to be valid code.

I get the next error

Traceback (most recent call last): y = 1 File "", line 1, in

File "/private/var/folders/6-/6-7wRVdAEhuqw8H28uS60U+++TI/-Tmp-/tmp3tx5on/code.py", line 20, in s2 = s2 + s1[k] File "element.pyx", line 1525, in sage.structure.element.RingElement.add (sage/structure/element.c:13476) File "coerce.pyx", line 797, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:7678) TypeError: unsupported operand parent(s) for '+': '' and 'Integer Ring'

THANKS SO MUCH

I found the last problem

I need to use, now it is working!

s2 = s2 + [s1[k]]

Upvotes: 0

Views: 947

Answers (1)

NPE
NPE

Reputation: 500933

You have unbalanced parentheses on the following line:

if (s1[k] + s1[y]) == ((s1[k] + (s1[k] + 2)):

I also noticed the use of x.is_prime(). I don't know if it's a Sage thing, but my standalone Python interpreter does not have an int.is_prime() method.

Finally, s1[len(s1)] will always raise an IndexError exception since s1's indices run from zero to len(s1)-1.

Upvotes: 2

Related Questions