We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTimportmathfromcollectionsimportdefaultdictn,pyth,minval=5*10**6,defaultdict(int),0foriinrange(1,int(math.sqrt(n))):forjinrange(1,i):ifmath.gcd(i,j)==1:a,b,c=i**2-j**2,2*i*j,i**2+j**2#Eulers formulaifnotmath.gcd(a,b,c)==1:#filter non primitive triplets to avoid duplicationcontinueifa+b+c>n:breakk=1whileTrue:pyth[k*(a+b+c)]+=1k+=1ifk*(a+b+c)>n:breakvals=[(0,0)]foriinsorted(pyth):ifpyth[i]>vals[-1][1]:vals.append((i,pyth[i]))defsearch(v,l,r,a):ifl==r:ifv<a[l][0]:return(a[l-1][0])return(a[l][0])m=(l+r)//2ifv<=a[m][0]:return(search(v,l,m,a))else:return(search(v,m+1,r,a))fora0inrange(int(input())):N=int(input())print(search(N,0,len(vals)-1,vals))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #39: Integer right triangles
You are viewing a single comment's thread. Return to all comments →
python 100%