Skip to content

Instantly share code, notes, and snippets.

@NTT123
Created August 8, 2017 14:04
Show Gist options
  • Save NTT123/fe567e6ed98f774a2472d551e06bd98d to your computer and use it in GitHub Desktop.
Save NTT123/fe567e6ed98f774a2472d551e06bd98d to your computer and use it in GitHub Desktop.
Print out square root of 2 with arbitrary precision
package main
//
// Print out square root of 2 with arbitrary precision
//
import (
"fmt"
"math/big"
)
func main() {
var n int64 = 2
a := big.NewInt(5 * n)
b := big.NewInt(5)
for i := 0; i < 100000; i++ {
if a.Cmp(b) >= 0 {
a.Sub(a, b)
b.Add(b, big.NewInt(10))
} else {
a.Mul(a, big.NewInt(100))
b.Sub(b, big.NewInt(5))
b.Mul(b, big.NewInt(10))
b.Add(b, big.NewInt(5))
}
}
fmt.Println(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment