from functools import reduce
def generate(current, factor, rem, scale=1):
while True:
current = (current * factor) % rem
if current % scale == 0: yield current
gen = (1 if (x & 0xFFFF) == (y & 0xFFFF) else 0 for x,y in
zip(generate(618,16807,2**31-1,4),
generate(814,48271,2**31-1,8)))
s = reduce(lambda x,y: x+y,(next(gen) for _ in range(5000000)))
print(s)