Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an iterative rational implementation for exact results #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Build go binary

frog
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/standupmaths/frog

go 1.12
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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))
}