Skip to content

Instantly share code, notes, and snippets.

@kassens
Forked from 140bytes/LICENSE.txt
Created May 23, 2011
Embed
What would you like to do?
Fibonacci in 52 bytes.
function(n){ // calculate the nth fibonacci number
for(
var a = 0, b = 1; // start with a = 0 and b = 1
n--; // run the loop n times
) b = a + (a = b); // assign a = b and b = a + b 'in parallel'
return a // return the result
}
function(n){for(var a=0,b=1;n--;)b=a+(a=b);return a}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jan Kassens <jan@kassens.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "fibonacci",
"description": "Fibonacci in 52 bytes.",
"keywords": [
"140bytes",
"fibonacci",
"math"
]
}
@williammalo
Copy link

williammalo commented Apr 11, 2012

save 2 bytes:
function(n,a,b){for(b=1;n--;)b=~~a+(a=b);return a}

@p01
Copy link

p01 commented Apr 11, 2012

facepalm How did we miss that ? Well done!

@rhysbrettbowen
Copy link

rhysbrettbowen commented May 25, 2012

function(n,a,b){for(a=0,b=1;n>1;n-=2,b+=a+=b);return n?b:a;};

longer but even faster (does two steps per loop so two assignments for two steps rather than two for one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment