Part 1


In [1]:
function next_value(seed, factor)
    (seed * factor) % 2147483647
end


Out[1]:
next_value (generic function with 1 method)

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]:
judge_count (generic function with 1 method)

In [3]:
@time judge_count(65, 8921)


  0.202172 seconds (2.61 k allocations: 138.314 KiB)
Out[3]:
588

In [4]:
@time judge_count(699, 124)


  0.195156 seconds (5 allocations: 176 bytes)
Out[4]:
600

Part 2


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]:
judge_count2 (generic function with 1 method)

In [6]:
@time judge_count2(65, 8921)


  0.788569 seconds (2.63 k allocations: 136.558 KiB)
Out[6]:
309

In [7]:
@time judge_count2(699, 124)


  0.778359 seconds (4 allocations: 160 bytes)
Out[7]:
313