implement period algoritm