Last active
December 21, 2016 17:43
-
-
Save triceam/3d6f69325418e3f1236ceb89b5995c6b to your computer and use it in GitHub Desktop.
OpenWhisk Fibonacci Action
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
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