-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
73 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// Calculate pi using Gregory-Leibniz series: (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) ... | ||
func calculatePi(iterations int) float64 { | ||
var result float64 = 0.0 | ||
var sign float64 = 1.0 | ||
var denominator float64 = 1.0 | ||
for i := 0; i < iterations; i++ { | ||
result = result + (sign * 4/denominator) | ||
denominator = denominator + 2 | ||
sign = -sign | ||
} | ||
return result | ||
} | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
log.Print("Pi calculator received a request.") | ||
iterations, err := strconv.Atoi(r.URL.Query()["iterations"][0]) | ||
if err != nil { | ||
fmt.Fprintf(w, "iterations parameter not valid\n") | ||
return | ||
} | ||
fmt.Fprintf(w, "%.10f\n", calculatePi(iterations)) | ||
} | ||
|
||
func main() { | ||
log.Print("Pi calculator started.") | ||
|
||
http.HandleFunc("/", handler) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
|
||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestCalculatePi(t *testing.T) { | ||
var pi float64 | ||
pi = calculatePi(20000000) | ||
if pi < 3.1415926 || pi >= 3.1415927 { | ||
t.Errorf("Value %.10f is incorrect", pi) | ||
} | ||
} |