Skip to content

Instantly share code, notes, and snippets.

@tianhengzhou
Created April 7, 2016 08:18
Show Gist options
  • Save tianhengzhou/d3cdb0f26fcced1d644d16d590c022e1 to your computer and use it in GitHub Desktop.
Save tianhengzhou/d3cdb0f26fcced1d644d16d590c022e1 to your computer and use it in GitHub Desktop.
<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