-
-
Save mckamey/979716 to your computer and use it in GitHub Desktop.
Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(n){for(var a=0,b=1,i=2;i<=n;)i++%2?b+=a:a+=b;return n%2?b:a} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "fib_tweet", | |
"description": "A tweet-sized, Fibonacci sequence generator.", | |
"keywords": [ | |
"Fibonacci", | |
"fib", | |
"tweet" | |
] | |
} |
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 =)