-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10179.go
67 lines (59 loc) · 927 Bytes
/
10179.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// UVa 10179 - Irreducable Basic Fractions
package main
import (
"fmt"
"os"
)
const max = 31622
var primes = sieve()
func sieve() []int {
p := make([]bool, max+1)
p[0], p[1] = true, true
for i := 2; i*i <= max; i++ {
if !p[i] {
for j := 2 * i; j <= max; j += i {
p[j] = true
}
}
}
var ps []int
for i, vi := range p {
if !vi {
ps = append(ps, i)
}
}
return ps
}
func solve(n int) int {
result := n
for _, prime := range primes {
if n < prime {
break
}
isFactor := false
for n%prime == 0 {
n /= prime
isFactor = true
}
if isFactor {
result = result / prime * (prime - 1)
}
}
if n != 1 {
result = result / n * (n - 1)
}
return result
}
func main() {
in, _ := os.Open("10179.in")
defer in.Close()
out, _ := os.Create("10179.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fprintln(out, solve(n))
}
}