ANSWERS: 5
  • 1,2,4,8,16,32,64,128.....etc
  • To do what? Do you need to generate a Fibonacci sequence? How many terms will you need to generate? The Fibonacci sequence runs like this: 0,1,1,2,3,5,8,13,21,34,55,89,144,... onwards The first term is zero. The second term is one. The third term, and all terms following, are the sum of the two terms immediately preceding it. I would suggest an array, dimensioned to the size you need. Populate the first and second terms, and use a looping structure to populate all the remaining terms you need. (For intense mathematical detail, go to http://en.wikipedia.org/wiki/Fibonacci_number )
  • But ,Which language? By JavaScript? You should tell us what you want to do , generate a sequence ,or get one term by the index, or else...
  • something like this: (untested, there may be script errors) <html> <head> <script type='text/javascript'><!-- // a fibonacci program. // The program generate fibonacci numbers, either singly, or as a sequence of numbers function getFibonacci(nstart,nstop) { // nstart and nstop are indexes for the place the // fibonacci number hold in the fibonacci series. var maxAllowed = 1000; // This program only allow indexes from 0 to 1000. // You can make the number higher, but beware, if you // make it to high the computer slows down, and your // browser may crash. var memory = new Array(); // here we remember the fibonacci numbers generated if (nstart < 0) nstart = 0; if (nstop == null) nstop = nstart; if (nstop > maxAllowed) nstop = maxAllowed; if (nstart > nstop) nstart = nstop; // enforce the limits on indexes. memory[0] = 0; memory[1] = 1; for (x=2; x<=nstop; x++) { memory[x] = memory[x-1] + memory[x-2]; } // now we have all the numbers wanted var output = new Array(); for (x=nstart; x<=nstop; x++) { output[x] = " " +x +":" +memory[x]; } alert( output.join("") ); // give result in an alertbox. } --> </script> </head> <body> indexes may go from 0 up to <script type='text/javascript'>document.write(maxAllowed);</script> <form name='testform'> first index; <input name='nstart' type='text'><br> last index; <input name='nstop' type='text'><br> <input name='calc' type='button' value='calculate' onclick="javascript:form=this.parentElement;getFibonacci(parseint(form.nstart.value),parseInt(form.nstop.value))"> </form> </body> </html>
  • Good for you! I want a vacation myself.

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy