Project Euler #26: Reciprocal cycles

  • + 0 comments

    100% divide will calculate the reccurance , by long division method by dividing 100* > n

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    small_frac = []
    
    def divide(n):
        nume = {1: 10, 2:100, 3:1000, 4:10000, 5: 100000}
        d = nume[len(str(n))]
        divq, rep = {}, '0'*(len(str(n))-1)
        l = len(rep)
        while True:
            d1 = d // n
            rep = rep + str(d1)
            d1 = d - n*d1
            d1 = d1 * 10
            #print(d1, l)
            if d1 in divq: 
                return(len(rep[divq[d1]:-1]))
            elif d % n == 0:
                return(0)
            divq[d1] = l
            d, l = d1, l+1
            
                
    def dec_frac():
        lastval = 0
        for i in range(2, 10001):
            val = divide(i)
            if val > lastval:
                small_frac.append(i)
                lastval = val
    
    dec_frac()
    
    #print(small_frac)
    
    def search(n, l, r, a):
        if l == r:
            if n <= a[l]:
                return(a[l-1])
            return(a[l])
        m = (l+r)//2
        if n <= a[m]:
            return(search(n, l, m, a))
        else:
            return(search(n, m+1, r, a))
         
    for a0 in range(int(input())):
        print( search(int(input()), 0, len(small_frac)-1, small_frac) )