# find period of y**a mod N # def period(y,N): z = y ans = 1 for i in range(2,N): z = y*z % N if z == y: return ans ans += 1 return 0