DEV Community

Douglas R Andreani
Douglas R Andreani

Posted on

Challenge: Write the recursive Fibonacci algorithm in a different language.

Hellow DEV community, how are you?

Can we challenge ourselves in this fun "game"?

Let's see how many languages we can enumerate by writing the Fibonacci recursive algorithm on the comments. Are you in?

EDIT: this is not about having the fast/better code, just to have some fun

Latest comments (51)

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

Argh, I was going to write a similar one in Elixir! I do like your join with the arrow!

Collapse
 
yorodm profile image
Yoandy Rodriguez Martinez • Edited

An Emacs Lisp version. Warning!! This will likely freeze your Emacs!!

(defun fib(n)
         (cond
          ((= n 0) 0)
          ((= n 1) 1)
          (t (+ (fib (- n 1)) (fib (- n 2))))))

UPDATE
Just realized it counts as a Common Lisp version too

Collapse
 
tarzan212 profile image
tarzan212

Lord, we got it, you're the best fibonnaci solver out there. Congratulations on this!

Collapse
 
marvodor profile image
Marvodor

Here's a Python variant for quite large numbers. Alas, due to recursion depth restrictions, any new calculated Fibonacci number should not have a larger sequence position than about 500 more than that of the highest already calculated.

def fib(a, F={}):
    if a in F:
        return F[a]
    if a < 2:
        return 1
    f = fib(a-1)
    F[a-1] = f
    return f + fib(a-2)

for i in range(0,50001,500):
    print("fib(%i) = %i"%(i,fib(i)))
Collapse
 
jvanbruegge profile image
Jan van Brügge • Edited

I have haskell here with the best form of recursion: Self-recursion!

fibs = 1:1:zipWith (+) fibs (tail fibs)
fib n = fibs !! n

Here fibs is an infinite List of all fibonacci numbers, the !! function returns the nth element of the list.

Collapse
 
jvanbruegge profile image
Jan van Brügge

I also have the boring tail recursive version here:

fib n = fibHelper n 1 1
    where fibHelper  0 a  _ = a
          fibHelper !n a !b = fibHelper (n-1) b (a+b)
Collapse
 
dwd profile image
Dave Cridland
#include <iostream>

constexpr long long fibonacci(long long i) {
    if (i <= 2) {
        return 1LL;
    }
    return fibonacci(i - 1) + fibonacci(i - 2);
}

int main() {
    std::cout << "Fibonacci of 27 is " << fibonacci(27) << std::endl;
    return 0;
}

Modern C++ can do it more simply, though - this uses constexpr to tell the compiler that it can calculate these at build time if it wants. Using a constexpr arg (here a literal), it probably will do, but we could also pass in a variable to make it calculate it at runtime.

Collapse
 
dwd profile image
Dave Cridland
#include <iostream>

template<long long i> long long fibonacci() {
    return fibonacci<i-1>() + fibonacci<i-2>();
}

template<> long long fibonacci<1>() {
    return 1;
}

template<> long long fibonacci<2>() {
    return 1;
}

int main() {
    std::cout << "Fibonacci of 27 is " << fibonacci<27>() << std::endl;
    return 0;
}

C++ can, of course, do it at build time with a bit of templates.

Here, we're definitely recursing - twice, because it's simpler. At runtime, it's just printing the value out that's been calculated by the compiler during the build.

Collapse
 
dwd profile image
Dave Cridland
def fib(arg):
  curr, prev = 1, 1
  n = int(arg)
  if n != arg or n <= 0:
    raise ValueError("Argument must be positive integer")
  if n <= 2:
    return 1
  for count in range(n - 2):
    curr, prev = curr + prev, curr
  return curr

Quick bit of Python. The error handling probably isn't complete, but Python has the useful benefit here that it has native, transparent, Big Number support - you can do fib(10000) if you want (or much higher if you have the memory).

But yeah, I just noticed Douglas wanted a recusrive algorithm, whereas I've done it iteratively without thinking. In general, iterative solutions will execute faster - though this isn't always the case.

Collapse
 
claytonflesher profile image
Clayton Flesher

As a Ruby fan, I'm sure glad Ruby has such a great ambassador in here.

Collapse
 
sam_ferree profile image
Sam Ferree

Funge++:

v   >:1-\2-101O\101O+B
 :1`|
    >$1B
>&:!#v_101O.55+,
     @

Some comments may only be visible to logged-in visitors. Sign in to view all comments.