In [1]:
function next_value(seed, factor)
(seed * factor) % 2147483647
end
Out[1]:
In [2]:
function judge_count(a, b)
a_factor = 16807
b_factor = 48271
c = 0
for i = 1:40000000
a = next_value(a, a_factor)
b = next_value(b, b_factor)
if a&0xFFFF == b&0xFFFF
c+=1
end
end
c
end
Out[2]:
In [3]:
@time judge_count(65, 8921)
Out[3]:
In [4]:
@time judge_count(699, 124)
Out[4]:
In [5]:
function next_value2(seed, factor, divisor)
v = (seed * factor) % 2147483647
while (v % divisor) != 0
v = (v * factor) % 2147483647
end
v
end
function judge_count2(a, b)
a_factor = 16807
b_factor = 48271
c = 0
for i = 1:5000000
a = next_value2(a, a_factor, 4)
b = next_value2(b, b_factor, 8)
if a&0xFFFF == b&0xFFFF
c+=1
end
end
c
end
Out[5]:
In [6]:
@time judge_count2(65, 8921)
Out[6]:
In [7]:
@time judge_count2(699, 124)
Out[7]: