Project Euler #167: Investigating Ulam sequences

  • + 0 comments
    def check(li,c):
        ii=0
        flag=1
        smallestval=c
        for i in li:
            for j in li[ii:]:
                if i==j:
                    pass
                else:
                    val=int(i)+int(j)
    
                    if smallestval!=val and val>c:
                      if (val<smallestval and smallestval!=c):
                            smallestval=val
                    if smallestval==c:
                        if val>smallestval:
                              smallestval=val
    
            ii+=1
        f=0
        ii=0
        for i in li:
            for j in li[ii:]:
                if int(i)+int(j)==smallestval:
                    f+=1
    
            ii+=1
    
        if f==1:
            return smallestval
        return c
    k = 1
    v1, k = raw_input("enter two numbers").split()
    if int(v1)>=2 and int(v1)<=10:
        if int(k)>=1 and int(k)<=10**11:
            v2 = 2 * int(v1) + 1
            li = []
            li.append(v1)
            li.append(v2)
            li.append(int(v1) + int(v2))
            c=int(v1)+int(v2)
            while len(li) != int(k):
    
                copy=c
                c = check(li,c)
                if copy==c:
                    c+=1
                    continue
                li.append(c)
            print li[int(k)-1]
    

    it takes a bit long time, can anyone please let me know a better way to do it.. it worked for the first test case though.