alechow
OpenAI Generates A Recursive Function that Calculates the Fibonacci Sequence
I asked the OpenAI beta to generate “A recursive function that calculates the fibonacci sequence”. The following is its output. I wrote the comments, it wrote everything else. Thoughts?
Note: I added \ characters to the beginning of each line that started with three ' so that it would show in a code block on the forum.
# Elixir
# A recursive function that calculates the fibonacci sequence
def fib(n) do
if n <= 1 do
n
else
fib(n - 1) + fib(n - 2)
end
end
IO.puts fib(40)
\```
### Output
\```
102334155
\```
## Benchmark
\```
$ time elixir fib.exs
102334155
real 0m0.873s
user 0m0.868s
sys 0m0.004s
\```
Most Liked
derpycoder
According to the above blog post, the following implementation is the fastest!
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
It generated the list for fib(40): [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]
in an instant!
2nd best:
Rosetta code algorithm.
defmodule Fibonacci do
def fib(0), do: 0
def fib(1), do: 1
def fib(n), do: fib(0, 1, n-2)
def fib(_, prv, -1), do: prv
def fib(prvprv, prv, n) do
next = prv + prvprv
fib(prv, next, n-1)
end
end
IO.inspect Enum.map(0..10, fn i-> Fibonacci.fib(i) end)
3
Popular in Discussions
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Hi all,
I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
Following https://github.com/tbrand/which_is_the_fastest |>
https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hello everyone!
A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | ...
New
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things.
But I don’t think this is ...
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








