Skip to content

Instantly share code, notes, and snippets.

@triceam
Last active Dec 21, 2016
Embed
What would you like to do?
OpenWhisk Fibonacci Action
var sequence = [1];
var invocations = 0;
function main(args) {
invocations = 0;
var int = parseInt(args.num);
//num is a zero-based index
return {
n:int,
value:fibonacci(int),
sequence: sequence.slice(0,int+1),
invocations: invocations
}
}
function fibonacci(num) {
invocations ++;
var result = 0;
if (sequence[num] != undefined) {
return sequence[num];
}
if (num <= 1 || isNaN(num)) {
result = 1;
} else {
result = fibonacci(num-1) + fibonacci(num-2);
}
if (num >= 0) {
sequence[num] = result;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment