from typing import Tuple
def four_squares(n: int) -> Tuple[int, int, int, int]:
for a in range(int(n**0.5) + 1):
for b in range(int((n - a**2)**0.5) + 1):
for c in range(int((n - a**2 - b**2)**0.5) + 1):
d = int((n - a**2 - b**2 - c**2)**0.5)
if a**2 + b**2 + c**2 + d**2 == n:
return a, b, c, d
return 0, 0, 0, 0
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
for i in [0, 1, 17, 33, 215, 333, 2**12-3, 1234567890, 106369249365575352836589875696130383747]:
a, b, c, d = four_squares(i)
error_msg = None
if type(a) is not int: error_msg = "1st square is not of type int"
if type(b) is not int: error_msg = "2nd square is not of type int"
if type(c) is not int: error_msg = "3rd square is not of type int"
if type(d) is not int: error_msg = "4th square is not of type int"
s = a * a + b * b + c * c + d * d
if s != i:
error_msg = f"Incorrect sum.\nSquares: [{a}, {b}, {c}, {d}]\nActual: {a * a + b * b + c * c + d * d}\nExpected: {i}"
if error_msg is not None:
print(error_msg)
else:
print("Решение принято")
from typing import Tuple
def four_squares(n: int) -> Tuple[int, int, int, int]:
for a in range(int(n**0.5) + 1):
a_sqr = a ** 2
for b in range(int((n - a_sqr)**0.5) + 1):
b_sqr = b ** 2
for c in range(int((n - a_sqr - b_sqr)**0.5) + 1):
c_sqr = c ** 2
d_sqr = n - a_sqr - b_sqr - c_sqr
d = int(d_sqr ** 0.5)
if a_sqr + b_sqr + c_sqr + d_sqr == n:
return a, b, c, d
return 0, 0, 0, 0
if __name__ == '__main__':
test_cases = [0, 1, 17, 33, 215, 333, 2**12-3, 1234567890, 106369249365575352836589875696130383747]
for i in test_cases:
a, b, c, d = four_squares(i)
squares = [a, b, c, d]
if not all(isinstance(s, int) for s in squares):
print(f"Error: squares are not of type int for n = {i}")
elif sum(s**2 for s in squares) != i:
print(f"Error: incorrect sum for n = {i}. Squares: {squares}")
else:
print(f"Solution accepted for n = {i}")