Skip to content

Instantly share code, notes, and snippets.

@dawnerd
Created November 13, 2010 21:55
Show Gist options
  • Save dawnerd/675682 to your computer and use it in GitHub Desktop.
Save dawnerd/675682 to your computer and use it in GitHub Desktop.
Outputs fibonacci numbers in JS
<html>
<head>
<title>fibonacci sequence</title>
</head>
<body>
<p>This will output a ton of numbers and might take a while.</p>
<div id="output"></div>
<script>
var o = '0, 1, ', f = 0, s = 1, n = 1, c = 1000;
while(c) n = (f+s), o += n + ', ', f = s, s = n, c--, document.getElementById('output').innerHTML = o;
/* longer version of the same code */
/*
var o = '0, 1, ';
var f = 0; //first number is always 0
var s = 1; //second number is always 1
var n = 1; //sum of f + s
var c = 1000; //how many times to run
while(c) {
n = (f + s); //next number
o += n + ', '; //buffers the output text for display
f = s; //sets the first number to be the second number
s = n; //sets the second number to be the next number
c--; //countdown
}
document.getElementById('output').innerHTML = o; //slightly changed from above for more performance boost.
*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment