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.
Here is my answer to the question. Arrived at this solution after looking at Euler's solution(https://sites.google.com/site/tpiezas/0020). But it is not running due to timeout problem :(
import math
from itertools import combinations
def perfect_square(n):
res = int(math.sqrt(n))
return res*res == n
def check_perfect(list):
p, q, r, s = list
if perfect_square( (p*p-q*q)*(r*r-s*s) ):
a = p*r + q*s
b = p*s - q*r
c = p*r - q*s
d = p*s - q*r
x = int((a*a + b*b)/2)
y = int((a*a - b*b)/2)
z = int((c*c - d*d)/2)
if(x>y and y>z):
print(p,q,r,s,x,y,z)
list = []
for i in range(1,int(math.pow(10,12)+1)):
if(perfect_square(i)):
list.append(i)
for i in combinations(list, 4):
check_perfect(i)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #142: Perfect Square Collection
You are viewing a single comment's thread. Return to all comments →
Here is my answer to the question. Arrived at this solution after looking at Euler's solution(https://sites.google.com/site/tpiezas/0020). But it is not running due to timeout problem :(
import math
from itertools import combinations
def perfect_square(n):
def check_perfect(list):
list = []
for i in range(1,int(math.pow(10,12)+1)):
for i in combinations(list, 4):