24 Neumanns Random Generator

Back to Problem Solutions forum

PingPong     2018-07-25 13:44:24

Neumann's Random Generator

Hello, i have problem with this exercise, I'm using Python

With simple input my code works fine, but when input is more complicated it does nothing My code:

#number of initial values
a = int(input())
#values
b = input().split()

for i in b:
    iterations = 0
    #Second value
    tr = int(((int(i)**2)/100)% 10000)
    iterations += 1

    #If second value != first value
    while tr != i:
        #Truncating
        tr1 = int(((tr**2)/100) % 10000)

        iterations += 1
        #If value is repeating, break
        if tr1 == tr or tr1 == int(i):
            break
        #If not, continue
        else:
            pass

        tr = tr1
    print(iterations, " ")

With input:

3

0001 4100 232

It prints: 2 4 18

But when input is sth like this:

1

5761

It does nothing. I supposed that it just needed more time, but after very long time with program running still nothing happenned

Quandray     2018-07-28 15:46:23
User avatar

I don't use python and I couldn't get your code to run with the example input.
However, I think your code isn't detecting the loop correctly - it could be looping on ANY of the previous values.
The answer for 5761 is less than 100, so I suggest you print out the first 100 values then stop and check them.

PingPong     2018-07-28 17:53:44

Thanks for the reply, my code was checking only if previous value was repeating, it wasn't checking every values that occured. I've made small modification and now it works fine :)

Please login and solve 5 problems to be able to post at forum