Skip to content

Instantly share code, notes, and snippets.

@samtsai
Forked from jswartwood/Premise.md
Created March 13, 2012 20:14
Show Gist options
  • Save samtsai/2031266 to your computer and use it in GitHub Desktop.
Save samtsai/2031266 to your computer and use it in GitHub Desktop.
AE Code Golf #1

Fibonacci Sequence

Classics series

Print the Fibonacci series (the first 10 values) separated by spaces. If you've ever taken a Computer Science class, you should be all over this one.

Rules

Solve the "Premise".

The smaller (bytes) the code, the better; if no one can shrink further, you win! It is good form to provide a (documented) non-minified version of your code, but it isn't required.

The programming language is dev's choice; however, languages should be compared both in isolation and overall (eg. your solution may be the best PHP solution, but Ruby may best it).

Add solutions in an appropriately suffixed solve file (eg. solve.js, solve.py).

Enjoy.

f=[0,1];for(i=2;i<10;i++)f[i]=f[i-1]+fb[i-2];console.log(f);
// 60 characters
fb=[];
for(i=0;i<10;i++)
fb[i]=fb[i-1]+fb[i-2] || i; // assign the left value when it can (ie values are defined) otherwise set to the index
// takes care of first two numbers in sequence (ie f0 = 0, f1 = 1)
console.log(fb,"for & ||");
// 70 characters
// Go a step further and assume first two numbers and pre-populate the array
fb=[0,1];
for(i=2;i<10;i++)
fb[i]=fb[i-1]+fb[i-2]; // no more || operator
console.log(fb,"for & prepopulate");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment