Project Euler #211: Divisor Square Sum

  • + 0 comments

    please help(terminated due to timeout) this code only able to pass test case 0

    import math
    
    def div(n):
        return set(x for tup in ([i, n//i] 
                    for i in range(1, int(n**0.5)+1) if n % i == 0) for x in tup)
    def sq(l):
        return [i ** 2 for i in l]
        
    def output(n,k):
        o=[]
        for i in range(2,n+1,1):
            n_div=div(i)
            sq_div=sq(n_div)
            snds=sum(sq_div)
            rtsq=math.sqrt(snds)
            x=math.ceil(rtsq)
            y=math.floor(rtsq)
            if(abs((x*x)-snds)<=k or abs((y*y)-snds)<=k):
                o.append(i)
        os=sum(o)
        return os+1
    
    n=int(input())
    cont = []
    for j in range(n):
        cont.append(list(map(int, input().rstrip().split())))
    for i in range(n):
        print(output(cont[i][0],cont[i][1]))