-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcount_semiprimes.rb
50 lines (42 loc) · 922 Bytes
/
count_semiprimes.rb
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
def count_semiprimes(n, p, q)
primes = primes(n)
semi_primes_count = Array.new(n + 1, 0)
primes.each do |prime1|
primes.each do |prime2|
break if prime1 * prime2 > n
semi_primes_count[prime1 * prime2] = 1
end
end
for i in 1..n
semi_primes_count[i] += semi_primes_count[i - 1]
end
p.zip(q).map do |from, to|
semi_primes_count[to] - semi_primes_count[from - 1]
end
end
def primes(n)
sieve = Array.new(n + 1, true)
sieve[0] = sieve[1] = false
i = 2
while i * i <= n
if sieve[i]
k = i * i
while k <= n
sieve[k] = false
k += i
end
end
i += 1
end
primes = []
sieve.each_with_index do |prime, i|
primes << i if prime
end
primes
end
require 'minitest/autorun'
class Tests < MiniTest::Unit::TestCase
def test_example_input
assert_equal [10, 4, 0], count_semiprimes(26, [1, 4, 16], [26, 10, 20])
end
end