Skip to content

Instantly share code, notes, and snippets.

@liketaurus
Last active May 1, 2019 17:59
Show Gist options
  • Save liketaurus/e0f548e40b3a88507e991d1900cfe797 to your computer and use it in GitHub Desktop.
Save liketaurus/e0f548e40b3a88507e991d1900cfe797 to your computer and use it in GitHub Desktop.
Azure Function to get a number from a Fibonacci sequence. Sample usage: https://functionName.azurewebsites.net/fibonacci?index=14
{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string index = req.Query["index"];
int n;
bool res = int.TryParse(index, out n);
return res == true && n>=0 && n<=40
? (ActionResult)new OkObjectResult($"{Fibonacci(n)}")
: new BadRequestObjectResult("Please provide a number between 0 and 40");
}
public static int Fibonacci(int n)
{
if (n == 0) return 0;
else if (n == 1) return 1;
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment