Project Euler #235: An Arithmetic Geometric sequence

  • + 0 comments

    I'm confused as to why I can only complete half of the test cases

    from copy import deepcopy
    
    def arGeoSeq(a1,d1,k1,r1):
        return (a1-d1*k1)*r1**(k1-1)
    
    def arGeoSeqDer(a1,d1,k1,r1):
        if(k1 == 1):
          return 0
        else:
          return (k1-1)*(a1-d1*k1)*r1**(k1-2)
            
    def arGeoSer(a2,d2,n2,r2):
        res = 0
        for i in range(1,n2+1):
            res += arGeoSeq(a2,d2,i,r2)
        return res
    
    def arGeoSerDer(a2,d2,n2,r2):
        res = 0
        for i in range(1,n2+1):
            res += arGeoSeqDer(a2,d2,i,r2)
        return res
        
    q = int(input())
    for i in range(q):
        temp = input().split(' ')
        a, d, n, x = int(temp[0]), int(temp[1]), int(temp[2]),int(temp[3])
        r = 1
        counter = 0
        while(True):
            temp = deepcopy(r)
            fx = arGeoSer(a,d,n,r) + x
            fxDer = arGeoSerDer(a,d,n,r) + x
            # print(counter,r,fx,fxDer)
            r = r - fx/fxDer
            if(r == temp):
                break
            counter += 1
        print(r)