-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path106.go
48 lines (43 loc) · 769 Bytes
/
106.go
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
47
48
// UVa 106 - Fermat vs. Pythagoras
package main
import (
"fmt"
"os"
)
func gcd(x, y int) int {
if y == 0 {
return x
}
return gcd(y, x%y)
}
func main() {
in, _ := os.Open("106.in")
defer in.Close()
out, _ := os.Create("106.out")
defer out.Close()
var n int
for {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
cache := make(map[int]bool)
cnt := 0
for i := 1; i*i <= n; i++ {
for j := i + 1; j*j <= n; j += 2 {
if gcd(i, j) == 1 {
a := j*j - i*i
b := 2 * i * j
c := i*i + j*j
if c > n {
break
}
cnt++
for times := 1; c*times <= n; times++ {
cache[a*times], cache[b*times], cache[c*times] = true, true, true
}
}
}
}
fmt.Fprintln(out, cnt, n-len(cache))
}
}