#Lagrange.py # every natural number is the sum of four squares (can incl. 0) #e.g. 45 = 0^2+2^2+4^2+5^2 also 0^2+0^2+3^2+6^2 #This program finds all the Lagrange sums for one integer import math n = int(input("Enter N: ")) lagrange_sums = set() #set of tuples of 4 sorted numbers #test: #lagrange_sums.add([1,2,3]) #can't have set of lists #lagrange_sums.add((1,2,3)) #can have set of tuples # tuple(list) #construct a tuple from a list OK for a in range(0,int(math.sqrt(n))+1): for b in range(0,int(math.sqrt(n))+1): for c in range(0,int(math.sqrt(n))+1): for d in range(0,int(math.sqrt(n))+1): if a*a+b*b+c*c+d*d == n: #sort the a,b,c,d so no dups if tuple(sorted([a,b,c,d])) not in lagrange_sums: print(n," ",a," ",b," ",c," ",d," ",(a*a+b*b+c*c+d*d)) lagrange_sums.add(tuple(sorted([a,b,c,d]))) print("#Lagrange sums for",n," is",len(lagrange_sums))