Project Euler #142: Perfect Square Collection

  • + 0 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):

    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)