defmodule Main do
def next_position(pos, offset, current) do
rem(pos + offset, current) + 1
end
def next_all(current, pos, new, offset) do
i = current + 1
next = next_position(pos, offset, i)
if next == 1, do: new = i
{ i, next, new }
end
def stream_generate(offset) do
first = { 1, next_position(0, offset, 1), 1 }
Stream.iterate(first,
fn {i, p, new} -> next_all(i,p,new,offset) end)
end
end
input = 348
num = 50000000
Main.stream_generate(input)
|> Stream.drop(num-1)
|> Stream.take(1)
|> Enum.to_list
|> IO.inspect