-
-
Save mckamey/979716 to your computer and use it in GitHub Desktop.
/** | |
* A tweet-sized, Fibonacci sequence generator. | |
* | |
* @param {number} n index within the sequence | |
* @return {number} Fibonacci value corresponding to the index | |
*/ | |
function(n) { | |
for (var a = 0, b = 1, i = 2; i <= n;) { | |
(i++ % 2) ? b += a : a += b; | |
} | |
return (n % 2) ? b : a; | |
} |
function(n){for(var a=0,b=1,i=2;i<=n;)i++%2?b+=a:a+=b;return n%2?b:a} |
Copyright (c) 2011 Stephen M. McKamey, http://mck.me | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
{ | |
"name": "fib_tweet", | |
"description": "A tweet-sized, Fibonacci sequence generator.", | |
"keywords": [ | |
"Fibonacci", | |
"fib", | |
"tweet" | |
] | |
} |
Google Closure Compiler compacts even smaller (69 bytes)!
function(n){for(var a=0,b=1,i=2;i<=n;)i++%2?b+=a:a+=b;return n%2?b:a}
Perhaps the other half should be used for unit tests. :)
62 bytes!
function(n,a,b){for(a=n&1,b=1-a;n>0;--n&1?b+=a:a+=b);return a}
Very nice! You can even move to a normal "var " statement since the args hack (",a,b") is the same length of 4 chars:
function(n){for(var a=n&1,b=1-a;n>0;--n&1?b+=a:a+=b);return a}
I guess you deliberately avoided doing it this way, but smaller for the same functionality anyway; 41b:
function f(n){return n>1?f(n-1)+f(n-2):n}
@wrayal - I would argue that the recursive implementation isn't the same functionality. Try f(50)
in both implementations and you'll see what I mean.
@wrayal: Also your recursive version pollutes the global namespace which is against the 3rd rule of 140byt.es. The 140byt.es "valid" recursive implementation of the Fibonacci function takes 62 bytes too:
function(){return function F(n){return n<2?n:F(n-1)+F(n-2)}}()
But of course the recursive approach will most likely throw some stack overflow or other OOM errors.
Did you guys see the 52bytes Fibonacci ? https://gist.github.com/986590
@mckamey: Hence my statement "I guess you deliberately avoided doing it this way...". However, I would contend that functionality is intrinstically context dependent: here the context is to the shortest code that works. My pollution of the global namespace is not so OK though I guess! ^_^
@p01: Long time no speak! I figured it wasn't such a crime since the same manner of pollution has even been used in some of Jed's codes without being flagged; the original script I had written down was the same code you gave but I couldn't see another way to knock out a spare byte, nor did I notice the 52byte version (thanks for the link!). I take your point about OOM errors though; this is a more significant functional difference =)
@wrayal: Indeed. Crazy busy. o_O Do you have a link to one of Jed's 140byt.es gists using a named function?
@p01: I thought I had a better example where it wasn't picked up in the comments, but the only one I could see immediately was this: https://gist.github.com/962814
Thanks for sharing. The argument is sound. I relayed it to the master/parent gist.
Shorter (73 bytes vs. original 85 bytes):
Maybe we can add extra features!