Konbor704

Konbor704

Fibonacci sequence that turns off computer.

Recently I started learning elixir for fun and as I’m new to programming in general it is not going too well.
Yesterday I found on the internet an exercise that required me to summarize all Fibonacci numbers less than 4_000_000. I tried doing it like that:

defmodule Fibonacci do
  def fibonacci(number) do
    Enum.reverse(fibonacci_do(number))
  end

  def fibonacci_do(1), do: [0]
  def fibonacci_do(2), do: [1 | fibonacci_do(1)]

  def fibonacci_do(number) when number > 2 do
    [x, y | _] = all = fibonacci_do(number - 1)
    [x + y | all]
  end
end

defmodule Solve do
  def lineOfFi(limit) do
    fib_numbers = Fibonacci.fibonacci_do(limit)
    Enum.sum(Enum.filter(fib_numbers, &(&1 < limit and rem(&1, 2) == 0)))
  end
end

result = Solve.lineOfFi(4_000_000)
IO.puts(result)

The first part is code that I found on the internet (it was supposed to be fast), as calculating the sequence itself completely defeated me. Second part I based on a problem that I solved few days ago.
Unfortunately, this code contains a hidden bug or is so unoptimized that after 5 or 6 minutes of running, it crushes my computer.
I wonder what could be done to improve optimization of this code, that it will be possible to run it at all.

Most Liked

technusm1

technusm1

Welcome to Elixir community @Konbor704, the problem here is that this code is trying to calculate 4 million fibonacci numbers, NOT fibonacci numbers less than 4_000_000.
See this line:

fib_numbers = Fibonacci.fibonacci_do(limit)

So, your Elixir program is doing a long computation because Fibonacci numbers tend to grow very fast.

If you want to calculate fibonacci numbers, you can use the Stream and Enum modules, specifically look at Stream.unfold:

# This one-liner creates an infinite "list" of numbers from
# which you can extract as many numbers as you want.
# The elements will be calculated on-demand by calling
# the passed function
Stream.unfold({0, 1}, fn {a, b} -> {a, {b, a + b}} end)

Now if you want the first 100 Fibonacci numbers, you can do:

Stream.unfold({0, 1}, fn {a, b} -> {a, {b, a + b}} end)
|> Enum.take(100)

If you want all fibonacci numbers less than 4 million, you can do:

Stream.unfold({0, 1}, fn {a, b} -> {a, {b, a + b}} end)
|> Enum.take_while(fn number -> number < 4_000_000 end)

Further, if you want to sum the numbers, you can pipe the above output to Enum.sum.

Enjoy learning Elixir and have fun solving problems. :slight_smile:

EDIT: I would also suggest looking at the Elixir pipe operator ( |> operator). It is one of the most loved features of Elixir and you’ll soon find out how you can improve your code to make it more readable and how it also helps to understand the parameter positions for a function in a given module (thus allowing you to easily understand and making it easier to memorize standard library module functions!)

Konbor704

Konbor704

Thank you so much for explanation.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement