kensremit

kensremit

Fibonacci with dynamic programming from imperative language need help

I know elixir is a functional programming language and this means can’t make a change state in some problems? Do I am missing something?
Can use any techniques to replace for solution dynamic programming Fibonacci? This is my solution in Golang

package main
import "fmt"

func fib(n int) int {
    // make initial array with size (n+1)
    answer := make([]int, n+1)
    
    // set answer[0] and answer[1] with default values
    answer[0] = 0
    answer[1] = 1
    
    // run dynamic programming and memorize steps
    // such that answer[i] = answer[i-1] + answer[i-2]
    // for each  2 <= i <= n
    for i := 2; i <= n; i++ {
        answer[i] = answer[i-1] + answer[i-2]   
    }
    
    // check answer array
    fmt.Println(answer)
    
    // fib(n) is last element of answer array
    return answer[n]
}

func main() {
   // test
   fmt.Println(fib(10))
   // output: [0 1 1 2 3 5 8 13 21 34 55]
   //         55         
}

Most Liked

josevalim

josevalim

Creator of Elixir

You will often see fibonnaci written according to its functional definition but as others pointed out, it is not very efficient as you compute the whole chain on every operation:

defmodule Fib do
  def fib(0), do: 0
  def fib(1), do: 1
  def fib(n), do: fib(n - 1) + fib(n - 2)
end

However, you can write a looping version, as in Go in Elixir. In Elixir we don’t have traditional loops, instead we use recursion. The other insight is that, in order to compute fibbonacci, we don’t need the whole array of results, only the last two, so we can implement it like this:

defmodule Fib do
  def fib(n), do: fib(n, {0, 1})

  defp fib(0, {current, _next}), do: current
  defp fib(n, {current, next}), do: fib(n - 1, {next, current + next})
end

It is worth reading more into recursion but one very useful exercise is to see how the execution goes manually. For example, for fib(5), this is what happens:

fib(4)
fib(4, {0, 1})
fib(3, {1, 1})
fib(2, {1, 2})
fib(1, {2, 3})
fib(0, {3, 5})
#=> when we reach 0, we match "defp fib(0, {current, _next}), do: current" and return 3

This is going to have the same perf complexity as the Go one.


This is an advanced topic, so please don’t consider it part of the answer, but there is another cool variant of the algorithm above which is to pack it into a stream. You can think of streams as lazy lists, which are generated on the fly, as needed! Here is a fib stream:

iex(3)> fib =                     
...(3)>   Stream.unfold({0, 1}, fn {current, next} ->
...(3)>     {current, {next, current + next}}
...(3)>   end)
#Function<63.34589589/2 in Stream.unfold/2>
iex(4)> Enum.at(fib, 4)
3
iex(5)> Enum.at(fib, 10)
55
iex(6)> Enum.at(fib, 100)
354224848179261915075

The cool think about the fib stream is that you are totally in control. If you want a single entry, you can have it. If you want a list, you got that too:

iex(7)> Enum.take(fib, 100)      
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,
 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229,
 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817,
 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733,
 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, ...]
17
Post #6
Exadra37

Exadra37

If you want to make the shift to functional programming then I strongly recommend that you first read this book:

This is the only book I know that spents a great deal of time educating you how to go from Object Orientated Programming and Procedural Programming to Functional Programming. @pragdave does an excellent job on making it click in our brains, can’t recommend it enough.

If you prefer video form, then he has a video course that does the same:

I have done both and that really helped me to let go away the old way of thinking about coding.

kokolegorille

kokolegorille

This is not possible, because there are no Arrays in Elixir, but linked List

In Elixir, You don’t access list element by index, because it’s expensive.

This is not possible, because i is mutated

Instead, You can have functions that transform input into output.

defmodule Demo do
  def fib(0), do: 0
  def fib(1), do: 1
  def fib(n), do: fib(n - 1) + fib(n - 2)
end

and if You want to cumulate, You can enumerate a range from 0 to 10 and pass the implementation details.

Enum.map(0..10, & Demo.fib(&1))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

This is not efficient, as the result is recalculated. But You can make it better with recursion.

Stratus3D

Stratus3D

Asdf Core Team

I wrote two blog posts about Fibonacci algorithms in Elixir, posts include benchmarks of all the implementations I evaluated:

This pretty well sums up what I found when I benchmarked the implementations I looked at in those blog posts.

chungwong

chungwong

Do you mean this https://gist.github.com/kyanny/2026028
There is a tail call optimised version too.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement