-
Notifications
You must be signed in to change notification settings - Fork 27
/
10539.go
62 lines (53 loc) · 984 Bytes
/
10539.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
// UVa 10539 - Almost Prime Numbers
package main
import (
"fmt"
"os"
)
const max = 1000000
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 isPrime(n int64) bool {
if n <= max {
return !primes[n]
}
for i := range primes {
if !primes[i] && int64(i*i) <= n && n%int64(i) == 0 {
return false
}
}
return true
}
func solve(low, high int64) int {
var cnt int
for i := int64(2); i*i <= high; i++ {
if i*i >= low && isPrime(i) {
for almost := i * i; almost <= high; almost = almost * i {
cnt++
}
}
}
return cnt
}
func main() {
in, _ := os.Open("10539.in")
defer in.Close()
out, _ := os.Create("10539.out")
defer out.Close()
var n, low, high int64
for fmt.Fscanf(in, "%d", &n); n > 0; n-- {
fmt.Fscanf(in, "%d%d", &low, &high)
fmt.Fprintln(out, solve(low, high))
}
}