Ask Your Question
1

Finding a sum of two squares that is itself the sum of two squares

asked 2021-08-01 03:39:33 +0100

brennan gravatar image

updated 2021-08-01 03:55:11 +0100

I am trying to write something that has a variable d that is the sum of two squares where one of the summands is also the sum of two squares. I tried something like

for d in srange(1,100):
  for e in srange(1,100):
     for f in srange(1,100):
          for g in srange(1,100):
              for h in srange(1,100):
                    if d==e^2+f^2 and e==g^2+h^2
                            print(d,e,f,g,h)

But it takes wayyyyyy to long to compute, is there a way to use 'from sage.rings.sum_of_squares import two_squares_pyx' to not brute force this?

edit retag flag offensive close merge delete

Comments

If you're looking for d = e^2 + f^2, do you want e to be a sum of two squares or do you want e^2 to be a sum of two squares? Your question asks for the latter (since e^2 is one of the summands) but your code looks for the former.

John Palmieri gravatar imageJohn Palmieri ( 2021-08-01 03:56:38 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-08-01 04:03:28 +0100

updated 2021-08-01 20:26:49 +0100

How about something like this:

from sage.rings.sum_of_squares import two_squares_pyx

for d in srange(1, 100):
    try:
        # See if d is a sum of two squares: d = e^2 + f^2
        e, f = two_squares_pyx(d)
        try:
            # Now test e.
            g, h = two_squares_pyx(e)
            print(d, e, f, g, h)
        except ValueError:
            # If e didn't work, try f.
            try:
                g, h = two_squares_pyx(f)
                print(d, e, f, g, h)
            except ValueError:
                continue
    except ValueError:
        continue
edit flag offensive delete link more

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: 2021-08-01 03:39:33 +0100

Seen: 299 times

Last updated: Aug 01 '21