Tag Archive: Faults

Oct
28
2011

Let’s correct to Google fibonacci algorithm

When we look at dartlang.org’s  home page and see an editor and code for Dart launcher with several examples what we do is first run and modify. For the first change to modify the example of fibonacci and changing 20 by 200 (the first thing we thought) left the browser blocked because it is an exponential function.

This is the original code:


int fib(int  n) {
  if (n < 2) return n;
  return fib(n - 1) + fib(n - 2);
}

main() {
  print('fib(20) = ${fib(20)}');
}

If you change it to print the complete list of the sequence of values ​​of the function you can see how it comes to crash the browser: Read the rest of this entry »

Permanent link to this article: http://www.dartexperience.com/en/2011/10/28/corrijamos-a-google-el-algoritmo-de-fibonacci/