Created
April 7, 2016 08:18
-
-
Save tianhengzhou/d3cdb0f26fcced1d644d16d590c022e1 to your computer and use it in GitHub Desktop.
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
| <xml><input id="input" type="text"> | |
| <button onclick="runFibonacci()">Submit</button> | |
| <div id="result">[]</div> | |
| <script> | |
| function runFibonacci(){ | |
| var N = document.getElementById('input').value, | |
| result = document.getElementById('result'), | |
| array = []; | |
| var f_sequence = fibonacci(); | |
| for (var i = 0; i < N; i++){ | |
| array.push(f_sequence(i)) | |
| } | |
| result.innerHTML = array.toString(); | |
| } | |
| function fibonacci(){ | |
| var array = {}; | |
| function f(N){ | |
| var value; | |
| if (N in array){ | |
| console.log('mem') | |
| value = array[N]; | |
| }else{ | |
| if (N === 0 || N === 1){ | |
| value = N | |
| }else{ | |
| value = f(N-1) + f(N-2); | |
| } | |
| array[N] = value; | |
| } | |
| return value | |
| } | |
| return f | |
| } | |
| </script> | |
| </xml> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment