# Hackerrank optimizing euler problem #1

**URL:** <https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367>\
**Category:** Coding Challenges\
**Tags:** learning-elixir\
**Created:** [May 12, 2019, 4:37pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367 "2019-05-12T16:37:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SMFloris](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smfloris/32/15366_2.png) [@SMFloris](https://forum.elixirforum.com/u/SMFloris)\
**Post date:** [May 12, 2019, 4:37pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/1 "2019-05-12T16:37:14Z")

</div>

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?

[Problem is here](https://www.hackerrank.com/contests/projecteuler/challenges/euler001/problem)

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.

```elixir
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

```

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [May 12, 2019, 4:58pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/2 "2019-05-12T16:58:54Z")

</div>

👋

Tasks seem unnecessary here. They probably cost more than the value they add over sequentially calling `dividedSum/2`.

---

<div class="post-metadata">

**Author:** ![AlchemistCamp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alchemistcamp/32/8823_2.png) [@AlchemistCamp](https://forum.elixirforum.com/u/AlchemistCamp)\
**Post date:** [May 12, 2019, 5:02pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/3 "2019-05-12T17:02:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SMFloris](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smfloris/32/15366_2.png) [@SMFloris](https://forum.elixirforum.com/u/SMFloris)\
**Post date:** [May 12, 2019, 6:03pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/4 "2019-05-12T18:03:09Z")

</div>

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:

```elixir
defmodule Solution do

  def dividedSum(n, dividend) do
    p = div(n-1, dividend)
    div(dividend * p * (p+1), 2)
  end

  def solveOne(n) do
    Solution.dividedSum(n, 3)+Solution.dividedSum(n, 5)-Solution.dividedSum(n, 15)
  end

  def solve() do
      {lines, _ }= IO.gets("") |> Integer.parse
      for _ <- 1..lines do
        {number, _} = IO.gets("") |> Integer.parse
        number
        |> Solution.solveOne
        |> IO.puts
      end
  end
end

Solution.solve

```

Thanks for the help! In the end, I overcomplicated things.

---

<div class="post-metadata">

**Author:** ![AlchemistCamp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alchemistcamp/32/8823_2.png) [@AlchemistCamp](https://forum.elixirforum.com/u/AlchemistCamp)\
**Post date:** [May 12, 2019, 6:38pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/5 "2019-05-12T18:38:17Z")

</div>

So I see! I was too quick to write that after seeing the `Enum.each` in `solve`.

Is the current code still not passing the last test?

Edit: My carelessness was the root cause, but maybe renaming `dividedSum` to something like `sum_of_multiples(n, multiple)` would make it clearer, also.

---

<div class="post-metadata">

**Author:** ![SMFloris](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smfloris/32/15366_2.png) [@SMFloris](https://forum.elixirforum.com/u/SMFloris)\
**Post date:** [May 12, 2019, 6:40pm UTC](https://forum.elixirforum.com/t/hackerrank-optimizing-euler-problem-1/22367/6 "2019-05-12T18:40:13Z")

</div>

The current code, now passes all tests 😃
