Hello everyone,
I spent my weekend optimising the first problem and learned allot in the process about Elixir. I got to 80 points, but still the third hidden test is timing out. Can anyone take a look at the code bellow and give me some pointers on how to improve the performance of the algorithm?
You can find the solution bellow; notice that I tried to parallelise as much as I could where it made sense (i.e. the solution is being computed as you input the numbers, in parallel). I don’t have allot of experience with Elixir so I would appreciate a helping hand.
defmodule Solution do
def calculateFinalSum([{:ok, a}, {:ok, b}, {:ok, c}]) do
a+b-c
end
def dividedSum(n, dividend) do
p = div(n-1, dividend)
div(dividend * p * (p+1), 2)
end
def solveOne(n) do
{number, _} = Integer.parse(n)
Task.async_stream([3,5,15], &(Solution.dividedSum(number, &1)))
|> Enum.to_list
|> Solution.calculateFinalSum
end
def solve() do
IO.gets("")
IO.stream(:stdio, :line)
|> Task.async_stream(&(Solution.solveOne(&1)))
|> Enum.each(fn {:ok, n} -> IO.puts(n) end)
end
end
Solution.solve


















