working check cyclicity code