Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
1

SyntaxError

asked 5 years ago

Sam_Na gravatar image

updated 5 years ago

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

Preview: (hide)

Comments

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 ( 5 years ago )

Weclome to Ask Sage. Thank you for your question.

slelievre gravatar imageslelievre ( 5 years ago )

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 ( 5 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 5 years ago

tmonteil gravatar image

updated 5 years ago

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}×{1,40}×{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.

Preview: (hide)
link

Comments

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 ( 5 years ago )

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 ( 5 years ago )

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: 5 years ago

Seen: 1,311 times

Last updated: May 04 '20