UVa 11634 - Generate random numbers Solution

def f(x):
    x*=x
    x%=1e6
    x//=100
    return x
def brent(x0):
    lam=1
    mu=0
    power=1
    tor=x0
    hare=f(x0)
    while hare!=tor:
        if power==lam:
            power*=2
            lam=0
            tor=hare
        hare=f(hare)
        lam+=1
    hare=x0
    tor=x0
    mu=0
    for i in range(lam):
        hare=f(hare)
    while hare!=tor:
        tor=f(tor)
        hare=f(hare)
        mu+=1
    return mu+lam
def Main():
    while True:
        n=int(input())
        if n==0:
            return 0
        print(brent(n))

if __name__=='__main__':
    Main()

Comments