Created
November 6, 2022 17:32
-
-
Save bryfry/51d3daa9174fea2a2f24230bd78f9c24 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import math | |
# x_n+1 : x_n * a + c % m | |
# x_0 : seed | |
examples = [ | |
{"a": 2, "c": 3, "m": 5 "seed": 1}, | |
{"a": 9656501935081, "c" : 73755019183457, "m" : 82852957687500, "seed" : 4719414047169}, | |
{"a": 179088735505681, "c" : 36802983139387, "m" : 182797296932250, "seed" : 53695366949483}, | |
{"a": 9682768815721, "c" : 4894180310753, "m" : 42130424626290, "seed" : 30179788743147}, | |
] | |
def igc(m: int, a: int, c: int, seed: int) -> int: | |
if math.gcd(a, m) != 1: | |
raise Exception(f"gcd(a,m) !: 1 for a:{a} m:{m}") | |
yield seed | |
last = seed | |
while True: | |
x = ((last * a) + c) % m | |
yield x | |
last = x | |
for e in examples: | |
for i, j in enumerate(igc(**e)): | |
print(i, j) | |
if i > 10: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment