Created
October 6, 2019 18:06
-
-
Save feltmax/d0d49df107456df783eb714346650d32 to your computer and use it in GitHub Desktop.
Floyd's cycle-finding algorithm in C/C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int f(int n) | |
{ | |
int p = 1, q = 1; | |
int s = 0, t = 0; // start : s, goal : t | |
while(1) | |
{ | |
p = (p*10)%n; | |
q = (q*10)%n; | |
q = (q*10)%n; | |
if(p==q) break; | |
} | |
if(p!=0) | |
{ | |
q = 1; | |
s = 1; | |
while(p!=q) | |
{ | |
s++; | |
p = (p*10)%n; | |
q = (q*10)%n; | |
} | |
q = (q*10)%n; | |
t = s; | |
while(p!=q) | |
{ | |
t++; | |
q = (q*10)%n; | |
} | |
} | |
return t-s+1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment