-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamiewong.rb
More file actions
75 lines (61 loc) · 1.04 KB
/
Copy pathjamiewong.rb
File metadata and controls
75 lines (61 loc) · 1.04 KB
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
68
69
70
71
72
73
74
75
#require 'benchmark'
primes = [2]
3.step(32000,2) do |i|
isprime = true
cap = Math.sqrt(i) + 1
primes.each do |p|
if (p >= cap)
break
end
if (i % p == 0)
isprime = false
break
end
end
if isprime
primes << i
end
end
numprimes = primes.length
T = gets.to_i
#Benchmark.bm do |x|
#x.report do
output = ""
t = 0
while t < T
#print "\n" if t > 0
line = gets.split(" ")
m = line[0].to_i
n = line[1].to_i
m = 2 if m < 2
cap = Math.sqrt(n) + 1
notprime = {}
i = 0
while i < numprimes
p = primes[i]
i+=1
if (p >= cap)
break
end
if (p >= m)
start = p*2
else
start = m + ((p - m % p)%p)
end
j = start
while j <= n
notprime[j] = true
j += p
end
end
i = m
while (i <= n)
if (notprime[i] == nil)
#print i,"\n"
end
i+=1
end
t += 1
end
#end
#end