defmodule Main do
def generate(current, factor, modulus, scale) do
next = rem(current * factor, modulus)
case rem(next, scale) do
0 -> next
_ -> generate(next,factor,modulus,scale)
end
end
def stream_generate(start, factor, modulus, scale) do
first = generate(start, factor, modulus, scale)
Stream.iterate(first,
&(generate(&1, factor, modulus, scale)))
end
end
use Bitwise
Stream.zip(
Main.stream_generate(618, 16807, 2147483647, 4),
Main.stream_generate(814, 48271, 2147483647, 8))
|> Stream.map(fn {x, y} -> (x &&& 0xFFFF) == (y &&& 0xFFFF) end)
|> Stream.map(fn match -> if match, do: 1, else: 0 end)
|> Stream.take(5000000)
|> Enum.sum
|> IO.puts