Is there an algorithmically better way of doing this?
import numpy as np
l=np.random.permutation(10)
p=list(range(10))
print(l,p)
i=0
k=0
prod=1
z=0
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
while z<10:
"""count how many times we have crossed (-1-ed) a number from p"""
k=0
while p[k]==-1 and k<9 :
"""find the first non -1 element in p (it's here that I feel that the code is especially clumsy) """
k=k+1
i=k
"""k is the first element in the cycle """
print(l[i])
p[k]=-1
z=z+1
j=1
while l[i]!=k:
"""go from i to l[i] until we get back to k, closing the cycle """
p[l[i]]=-1
z=z+1
i=l[i]
j+=1
"""g is just used to find the lcm """
print(l[i])
prod=lcm(prod,j)
print('------')
print(prod)