Fibonacci in Every Language

Jeremy Grifski - May 22 '19 - - Dev Community

Once again, I thought it would be fun to kick off one of these challenges in as many programming languages as possible. Last time, we tried our hand at Fizz Buzz in Every Language. If you haven't gotten a chance to share a solution in your favorite language, go for it! Otherwise, we're going to tackle Fibonacci in Every Language today.

The Challenge

As many of you are probably aware, Fibonacci is a term often associated with a particular sequence of numbers where the nth term is the sum of the previous two terms—or more formally: Fn = Fn-1 + Fn-2. For our purposes, we'll start the sequence with a pair of ones:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

The goal in this challenge is to write a program in a language of your choice to output the sequence up to the nth term. In other words, the program should be able to be executed like:

./fib 5
Enter fullscreen mode Exit fullscreen mode

and, output the sequence above up to the fifth term:

1, 1, 2, 3, 5
Enter fullscreen mode Exit fullscreen mode

If you're feeling adventurous, you could try making your program conform to the requirements outlined in the Sample Programs repo. In addition to outputting the sequence, you should also output the index of each term:

1: 1
2: 1
3: 2
4: 3
5: 5
Enter fullscreen mode Exit fullscreen mode

Either style is fine for the purposes of this challenge. That said, if you follow the second convention, it'll be much easier to transition into the repo!

The Solution

As always, I'll kick off the fun with a solution in Haskell brought to you by a couple of our contributors (Parker Johansen and Panagiotis Georgiadis):

module Main where

import System.Environment
import Text.Read

fibonacci :: Int -> [Integer]
fibonacci = flip (take) fibs

fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

headMaybe :: [a] -> Maybe a
headMaybe []     = Nothing
headMaybe (x:xs) = Just x

-- Takes a list of values and returns a list of strings in the format "ONE_BASED_INDEX: VALUE"
printWithIndex :: (Show a) => [a] -> [[Char]]
printWithIndex = zipWith (\i x -> (show i) ++ ": " ++ (show x)) [1..]


-- Prints out the first N numbers from the fibonacci sequence
-- where N equals to the first command line argument.
main :: IO ()
main = do
  args <- getArgs
  let n = headMaybe args
  case n >>= readMaybe of
    Nothing -> putStrLn "Usage: please input the count of fibonacci numbers to output"
    Just n  -> mapM_ (putStrLn) $ (printWithIndex . fibonacci) n
Enter fullscreen mode Exit fullscreen mode

For those of you that maybe haven't seen functional programming before, this solution probably looks pretty wild. That said, I think Haskell is far more readable than anything Lisp-based, so you can probably get the gist of how this works.

Unfortunately, we don't currently have this solution documented in our collection, so maybe one of you can help out. We'd appreciate the support!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player