SMFloris

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?

Problem is here

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

Most Liked

idi527

idi527

:waving_hand:

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

AlchemistCamp

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

SMFloris OP

The current code, now passes all tests :smiley:

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews