Last active
February 25, 2024 23:38
-
-
Save linj121/3a32e059a7c47a6a57bcfc323b6c7063 to your computer and use it in GitHub Desktop.
Floyd Cycle Detection
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
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