-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10023.go
More file actions
46 lines (40 loc) · 697 Bytes
/
10023.go
File metadata and controls
46 lines (40 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// UVa 10023 - Square root
package main
import (
"fmt"
"math/big"
"os"
)
var two = big.NewInt(2)
func sqrt(num string) *big.Int {
var n, m, h, tmp big.Int
n.SetString(num, 10)
h.Set(&n)
l := big.NewInt(1)
for {
m.Add(l, &h).Div(&m, two)
switch cmp := tmp.Mul(&m, &m).Cmp(&n); {
case cmp == 0:
return &m
case cmp < 0:
l.Set(&m)
default:
h.Set(&m)
}
}
}
func main() {
in, _ := os.Open("10023.in")
defer in.Close()
out, _ := os.Create("10023.out")
defer out.Close()
var kase int
var line string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "\n%s", &line)
fmt.Fprintln(out, sqrt(line))
if kase > 1 {
fmt.Fprintln(out)
}
}
}