# Fibonacci Numbers

# Fibonacci Numbers

- TL
taammas_hrb + 0 comments It doesnt look so pretty but it works. I dont understand why I needed to 'hack' this to make it work though, obviously I started with fib 0 and 1 = to 1 but had to 'hack' it since it was off by 2

Haskell:

fib 0 = 0 fib 1 = 0 fib 2 = 1 fib 3 = 1 fib n = fib (n-1) + fib (n-2)

Anachor + 2 comments It's sad that for each challenge the majority of comments take the form:

`Why is ---------- language not on the list. OR Please enable ----------- language.`

Moderators should make some kind of Disclaimer or something about this.

vivmbbsHackerRank Admin + 0 comments That's not good. Could you please tell me which thread has this? we enable pretty much all available languages for a challenge

abhiranjan + 1 comment Hi @Anachor, you are in functioal programming domain. Here we only support only those languages which have extensive support for functional paradigm, eg, Haskell, Scala, Clojure etc.

Probably you are looking for Algorithms domain. There all the languages are enabled.cityseeker + 1 comment Lol yeah cause doing recursion is not an Algorithm? These domains are cool all the way up till you restrict what language i can write in depending on the domain. Are you gonna make me write in Fortran in the Mathematics domain? Forcing people to use ridiculously unpopular functional programming languages to learn recursion is just well ... silly. Just copy and paste your problems and practice in your own ide in whatever language you want.

lvsz_ + 0 comments I bet you also can't grasp why they don't allow Python in the Java section.

abratashov + 0 comments Elixir solution of the 'Fibonacci Numbers'

defmodule Solution do def fibonacci_n(n \\ 0) do case n do 1 -> 0 2 -> 1 n -> fibonacci_n(n-1) + fibonacci_n(n-2) end end def main do IO.gets("") |> String.trim |> String.to_integer |> fibonacci_n |> IO.puts end end Solution.main

- JI
prosopopoeia + 0 comments Some ugly Clojure:

(println ((fn fibs [num] (let [sum 0] (if (= 1 num) 0 (if (= 2 num) 1 (+ (fibs (dec num)) (fibs (- num 2))))))) (Integer/parseInt (clojure.string/trim (read-line)))))

Would this be possible using Clojure's loop-recur functions? I played with a bit and decided it couldn't be done, anbody have any insights on whether that is accurate?

koldbyte + 0 comments Looks a bit hacky but simple. Scala.

def fibonacci(x:Int):Int = { if(x <= 1) x else { (1 to x-1).foldLeft((0, 1)) { case ((a,b), i) => (b, a+b) }._1 } }

- E
eric_li_canada + 0 comments Tail recursive in Scala

def fibonacci(x: Int): Int = { val initialValue = 0 // f(1) == 0 val initNext = 1 // f(2) == 1 val initialIndex = 1 // 1 < N <= 40 @tailrec def iter(cur: Int, next: Int, acc: Int): Int = { if (acc == x) cur else iter(next, cur + next, acc + 1) } iter(initialValue, initialNext, initialIndex) }

- C
ProofOfPizza + 0 comments fib :: Int -> Int fib n = fiblist [1,0] where fiblist (x:y:zs) = if length (x:y:zs) < n then fiblist (y+x:x:y:zs) else x

- FH
fheil0815 + 0 comments fibh :: Int -> Int -> Int -> Int fibh 1 a b = a+b fibh n a b = fibh (n-1) b (a+b) fib 1 = 0 fib 2 = 1 fib n = fibh (n-2) 0 1

i feel dirty for using recursion like this, but the runtime should be fine...

shami4146 + 3 comments What's wrong with this code??

def fibonacci(n : Int) : Long = { if( n == 0 ){ return 0; } else if( n == 1 ){ return 1; } else{ return fibonacci(n-1) + fibonacci(n-2); } }

jacob_tomaw + 0 comments This problem uses an incorrect definition of fibonacci unfortunatly. For this problem fib(1) != 1, instead fib(1) == 0;

I think you solution is correct for standard fibonacci.

paulpach + 0 comments As Jacob said, the problem is unusual in the sense that it starts with 1 instead of 0. Your solution is correct if you print fibonacci n-1

Although correct, this implementation is inneficient. If you calculate fibonnaci(5) you will end up calculating fibonnaci(3) twice. For larger numbers the tree will grow in O(2^n) performance.

A better way is to calculate fibonnaci from 0 upwards and stopping at the number you want. This way you only calculate each fibonnaci once and you solve your problem in O(n). For example:

-- never ending series of fibonacci numbers fibSerie a b = a : fibSerie b (a+b) -- take the n - 1 number in the series fib n = (fibSerie 0 1) !! (n - 1)

im_mohsin + 0 comments Its too late to reply but let me just answer it anyway. According to problem statement,You have return 1 if n is 0 and return 2 if n is 1.

dadwalharkamal + 2 comments This is not cool.

its a simple program working fine in scala . but the same code when i pasted here it says for the input 5 expected output is 3 how come?

- FL
hackerrank152 + 0 comments off-by-one error, ie you start with f(0) = 0, f(1) = 1, ... while this task starts with f(1) = 0, f(2) = 1, ...

drakemp + 0 comments In the main, you can fix it by adding minus one

Sort 23 Discussions, By:

Please Login in order to post a comment