-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionapp.csx
60 lines (48 loc) · 1.65 KB
/
functionapp.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#r "Newtonsoft.Json"
using System.Net;
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string digits = req.Query["digits"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
digits = digits ?? data?.digits;
try
{
int nrOfDigits=Int32.Parse(digits);
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
string results=PiNumberFinder(nrOfDigits);
stopwatch.Stop();
TimeSpan ts = stopwatch.Elapsed;
return (ActionResult)new OkObjectResult($"Time:{ts}, PI:{results} ");
}
catch (Exception ex)
{
return new BadRequestObjectResult("Wrong number in query!");
}
/*return digits != null
? (ActionResult)new OkObjectResult($"Hello, {digits}")
: new BadRequestObjectResult("Please pass a digits to calculate PI");*/
}
public static string PiNumberFinder(int digitNumber)
{
string piNumber = "3,";
int dividedBy = 11080585;
int divisor = 78256779;
int result;
for (int i = 0; i < digitNumber; i++)
{
if (dividedBy < divisor)
dividedBy *= 10;
result = dividedBy / divisor;
string resultString = result.ToString();
piNumber += resultString;
dividedBy = dividedBy - divisor * result;
}
return piNumber;
}