Ask Your Question

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

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

brennan gravatar image

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

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

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


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 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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

How about something like this:

from sage.rings.sum_of_squares import two_squares_pyx

for d in srange(1, 100):
        # See if d is a sum of two squares: d = e^2 + f^2
        e, f = two_squares_pyx(d)
            # Now test e.
            g, h = two_squares_pyx(e)
            print(d, e, f, g, h)
        except ValueError:
            # If e didn't work, try f.
                g, h = two_squares_pyx(f)
                print(d, e, f, g, h)
            except ValueError:
    except ValueError:
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


Asked: 2021-08-01 03:39:33 +0200

Seen: 210 times

Last updated: Aug 01 '21