# SyntaxError

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 :)

var("a,b,n")
f(n)=n^2+a*n+b:
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 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 ";"

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.

Sort by » oldest newest most voted

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 ?

EDIT

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):
print(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.

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 :)

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