尤拉計劃723:畢達哥拉斯四邊形

lt發表於2020-07-05

enter image description here

畢達哥拉斯四邊形
測試程式

def f(n):
 r=math.floor(math.sqrt(n))
 x=range(-r,r+1)
 y=range(-r,r+1)
 p=[(i,j) for i in x for j in y  if i*i+j*j==n]
 def ax(p):
  x,y=p[0],p[1]
  if x>0 and y>=0 :return (1, -x)
  if x<=0 and y>=0:return (2, -x)
  if x<=0 and y<0 :return (3, x)
  if x>0 and y<0  :return (4, x)
 p.sort(key=ax)
 lenp=len(p)
 s=0
 for i in range(0,lenp):
  for j in range(i+1,lenp):
   for k in range(j+1,lenp):
    for a in range(k+1,lenp):
     t= p[i][0]*p[j][0]+p[j][0]*p[k][0]+p[k][0]*p[a][0]+p[a][0]*p[i][0]
     t+=p[i][1]*p[j][1]+p[j][1]*p[k][1]+p[k][1]*p[a][1]+p[a][1]*p[i][1]
     if t==0:s+=1
 return s

import itertools

def S(L): #L=[(5,2),(13,1)],L=[(5,1),(13,1),(17,1)]
 L1=[]
 for x in L:
  L1.append( [x[0]**i for i in range(x[1]+1)])
 s=0 
 for i in itertools.product(*L1):
  m=1
  for j in i:  m*=j
  s+=f(m)
 return s