Skip to content

Instantly share code, notes, and snippets.

@wrayal
Forked from 140bytes/LICENSE.txt
Created May 30, 2011 10:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrayal/998671 to your computer and use it in GitHub Desktop.
Save wrayal/998671 to your computer and use it in GitHub Desktop.
Fibonacci in 41 bytes

41b Fibonacci

This uses the simple but potentially slow recursive implementation of Fibonacci. Outside a 'short code' contest or a pedagogical setting, nobody in their right minds would code it this way; however it saves:

44b versus first implementation by @mckamey (http://gist.github.com/979716 21b versus "p01's condensation of the same algo (same gist) 21b versus the unnamed version of the same code 11b versus @kassens' very nice variable swapping version (https://gist.github.com/986590)

I'm sure there's probably a couple more bytes to be taken out somewhere!

function f(n){ //Clarification: we ARE allowed named functions
return n>1? //Check: are we at a 'generic stage' or 'end stage'?
f(n-1)+f(n-2) //Generic: return sum of the two previous terms
:n //return 0 for zeroeth term, 1 for 1st term
}
alert(f(40))
function f(n){return n>1?f(n-1)+f(n-2):n}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
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": "41bFib",
"description": "calculate fibonacci....reeeeealllly slowly",
"keywords": [
"140bytes",
"Fibonacci",
"recursive"
]
}
@atesgoral
Copy link

I don't think you need the explicit myFn= part at all (even though you're clearly not counting it towards the 41b)...

@wrayal
Copy link
Author

wrayal commented May 30, 2011

Thanks! Sorry, I'm not sure how that slipped in there >_<

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