Skip to content

Instantly share code, notes, and snippets.

@feltmax
Created October 6, 2019 18:06
Show Gist options
  • Save feltmax/d0d49df107456df783eb714346650d32 to your computer and use it in GitHub Desktop.
Save feltmax/d0d49df107456df783eb714346650d32 to your computer and use it in GitHub Desktop.
Floyd's cycle-finding algorithm in C/C++
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