Skip to content

Instantly share code, notes, and snippets.

@linj121
Last active February 25, 2024 23:38
Show Gist options
  • Save linj121/3a32e059a7c47a6a57bcfc323b6c7063 to your computer and use it in GitHub Desktop.
Save linj121/3a32e059a7c47a6a57bcfc323b6c7063 to your computer and use it in GitHub Desktop.
Floyd Cycle Detection
class FloydCycleModel:
def __init__(self, cycleLength: int, fasterSpeed: int, slowerSpeed: int) -> None:
self.CycleLength = cycleLength
self.FasterSpeed = fasterSpeed
self.SlowerSpeed = slowerSpeed
def calcIterations(self, n: int) -> int:
return n * self.CycleLength / (self.FasterSpeed - self.SlowerSpeed)
def argmin(self) -> int:
n = 1
while self.calcIterations(n) % 1 != 0:
n += 1
return n
def getNumOfIterations(self) -> int:
return self.calcIterations(self.argmin())
if __name__ == "__main__":
floydCycle = FloydCycleModel(4, 2, 1)
print(floydCycle.getNumOfIterations())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment