SMFloris
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
Trending in Challenges
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
Tasks seem unnecessary here. They probably cost more than the value they add over sequentially calling
dividedSum/2.AlchemistCamp
Most Project Euler questions revolve around mathematical insight. This particular question has an O(1) solution!
A hint to point you in the right direction is to consider “triangle numbers”, or the 3rd line of Pascal’s triangle. The sum of all the numbers between 0 and 100 can be thought of as (0 + 100) + (1 + 99) + (2 + 98) + (3 + 97) … + (49 + 51) + 50. Using this sort of procedure, you can find the sum of any sequence of whole numbers from 0 to n by this equation:
(n^2)/2 + n/2, which is the same as n(n + 1) / 2
Can you think of a way to find the sum of all the numbers from 0 to n that are divisible by 3? Or the numbers divisible by 5? Or 15?
SMFloris
Hello,
@AlchemistCamp, This is exactly what I am using here, its a bit hidden by the optimizations I tried to make. If you read carefully, I do: 3*(1+2+…+n/3)+5*(1+2+…+n/5)-15*(1+2+…+n/15), where 1+2+…+n/3 and the other series are calculated using the formula Sn = n*(n+1)/2.
@idi527, Indeed, you were correct. Getting rid of the tasks solved my issues. This was the winning solution:
Thanks for the help! In the end, I overcomplicated things.
AlchemistCamp
So I see! I was too quick to write that after seeing the
Enum.eachinsolve.Is the current code still not passing the last test?
Edit: My carelessness was the root cause, but maybe renaming
dividedSumto something likesum_of_multiples(n, multiple)would make it clearer, also.SMFloris
The current code, now passes all tests