Created
August 8, 2017 14:04
-
-
Save NTT123/fe567e6ed98f774a2472d551e06bd98d to your computer and use it in GitHub Desktop.
Print out square root of 2 with arbitrary precision
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
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