# Finding a sum of two squares that is itself the sum of two squares 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 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.

Sort by » oldest newest most voted 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

more