R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

{Last Two Fibs}

The Question

This is the Fibonacci series

1, 1, 2, 3, 5, 8, 10 ...

Write an efficient algorithm that will take as input an input n and will return the two numbers directly proceeding the nth Fibonacci number.

 

Examples

nthFib(1) => [undefined, undefined]

nthFib(2) => [undefined , 1]

nthFib(3) => [1,1]

nthFib(4) => [1,2]

nthFib(5) => [2,3] 

 

 Breakdown

  1. Realize you need to write a for loop that both generates the Fibonacci series and keeps track of the last two numbers.
  2. Optimize solution with memoization

Solution

Memoized Solution

Have a repl

http://repl.it/o19 - regular

http://repl.it/o1h - memoized

 

Reacto:Last Two Fibs

By Benjamin Conant

Reacto:Last Two Fibs

  • 1,487