diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1720fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Build go binary + +frog \ No newline at end of file diff --git a/README.md b/README.md index fda46c1..7e6cc56 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # frog_problem Here is the frog problem code I wrote in an Edinburgh pub. -Feel free to make better version. This one has not changed since I wrote it. V1 was the same code but saved before I changed it to work for different distances so I have not bothered upload it as well (it was still very unifinished). +Feel free to make better version. This one has not changed since I wrote it. V1 +was the same code but saved before I changed it to work for different distances +so I have not bothered upload it as well (it was still very unifinished). -Video is here: https://youtu.be/ZLTyX4zL2Fc +Video is here: https://youtu.be/ZLTyX4zL2Fc \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ea5fc00 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/standupmaths/frog + +go 1.12 diff --git a/main.go b/main.go new file mode 100644 index 0000000..10b03c8 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "math/big" + "os" + "strconv" +) + +func main() { + // Parse input value from command line args. Do simple validation. + n := 10 + if len(os.Args) > 1 { + parsed, err := strconv.ParseInt(os.Args[1], 10, 64) + if err != nil || parsed < 1 { + panic("Input target must be a positive integer") + } + n = int(parsed) + } + + sum := big.NewRat(0, 1) + + for i := 1; i <= n; i++ { + sum = sum.Add(sum, big.NewRat(1, int64(i))) + } + + fmt.Printf("Frog(%d) = %s == %s\n", n, sum.String(), sum.FloatString(10)) +}