-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path11466.go
65 lines (57 loc) · 879 Bytes
/
11466.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
// UVa 11466 - Largest Prime Divisor
package main
import (
"fmt"
"os"
)
const max = 10000000
var primes = sieve()
func sieve() []bool {
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
}
}
}
return p
}
func solve(n int64) int64 {
if n < 0 {
n = -n
}
var count, largest int64
for i := int64(0); i <= int64(max) && i <= n; i++ {
if !primes[i] {
for ; n%i == 0; n /= i {
if largest != i {
count++
largest = i
}
}
}
}
if n != 1 {
count++
largest = n
}
if count == 1 {
return -1
}
return largest
}
func main() {
in, _ := os.Open("11466.in")
defer in.Close()
out, _ := os.Create("11466.out")
defer out.Close()
var n int64
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fprintln(out, solve(n))
}
}