Ask Your Question


asked 2020-05-03 12:46:41 +0200

Sam_Na gravatar image

updated 2020-05-04 13:22:34 +0200

Hello everyone, I'using Sage for three weeks now and I got a problem.I have been looking for the error in this code for about 3 days and I can't figure it out. Maybe you can help me :)

for a in range(1,41):
for b in range(1,41):
if f(n) in ZZ
for n in range(0,41)
if is_prime(f(n)):
print(a); print(b)

I'm getting the error:

__tmp__=var("n"); f = symbolic_expression(n**Integer(2)+a*n+b:).function(n)
SyntaxError: invalid syntax

I hope you can help me :)

edit retag flag offensive close merge delete


There is no need at all for final ";" in sage. And branching commands (if, for,and so on) require a final colon ":", not a final ";"

FrédéricC gravatar imageFrédéricC ( 2020-05-03 15:18:28 +0200 )edit

Weclome to Ask Sage. Thank you for your question.

slelievre gravatar imageslelievre ( 2020-05-03 15:21:58 +0200 )edit

It seems the indentation was lost, please edit your question to fix that.

Also please remove semicolons at the end of lines, they are not needed.

slelievre gravatar imageslelievre ( 2020-05-03 15:22:00 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-05-03 15:10:44 +0200

tmonteil gravatar image

updated 2020-05-04 14:29:41 +0200

You seem to be confusing symbolic variables with Python names. Since you plan to let a,b,n take som precise integer values, you should better define f = n^2+a*n+b only when a, b, n get a value, that is inside the loop.

I could help to rewrite the code, but I am not surre about your objective, do you want to find all tuples (a,b,n) in $\{1,40\} \times \{1,40\} \times \{0,40\}$ such that n^2+a*n+b is prime ?

Also, what are you trying to test when you write if f(n) in ZZ ?


To test a couple (a,b), you can define the function:

def test(a, b):
    return all(is_prime(n^2+a*n+b) for n in range(1,41))

Now, you can run your test for the first pairs a,b:

for a in range(100):
    for b in range(100):
        if test(a,b):

Unfortunately, you will not find any pair in that range.

Note that when a,b,n are integers, n^2+a*n+b is also an integer.

edit flag offensive delete link more


Thanks for your answer. I am looking for a quadratic polynomial f(n)=n^2+a*x+b with a and b from the natural numbers. so when I write if f(n) in ZZ I'm trying to get the natural numbers
f(n) should then give me only prime numbers for all n values between 1 and 40. So I'm not sure if I have to define a and b to beZZ? Thanks a lot :)

Sam_Na gravatar imageSam_Na ( 2020-05-04 13:21:34 +0200 )edit

Ahh thank you very much. It worked well!! :D Okay now I'm thinking of doing a kind of list to get the is_prime()in range(1,41) for both a and b. So I have a list like True True False . . . Till 41

Do you have any tips to create a list like that? :) Thank you very much

Sam_Na gravatar imageSam_Na ( 2020-05-04 19:56:34 +0200 )edit

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


Asked: 2020-05-03 12:46:41 +0200

Seen: 1,123 times

Last updated: May 04 '20