Skip to content
Advertisement

Count the semiprime numbers in the given range [a..b]

I am solving Codility problem CountSemiprimes: Count the semiprime numbers in the given range [a..b].

Task description

A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.

A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.

Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..50,000];
  • M is an integer within the range [1..30,000];
  • each element of arrays P, Q is an integer within the range [1..N]; P[i] ≤ Q[i].

My solution

My current score is 66% and problem is preformance for large data set:

  • large random, length = ~30,000
  • all max ranges

Test says, that it should take about 2sec, but my solution takes over 7sec.

This is my current solution

JavaScript

Any ideas about improving performance? My last one was to remove Set for holding semi-primes with array. It helped me to solve couple of performance tests.

Advertisement

Answer

A Java solution which scores 100% is as follow:

  • Find the set of prime numbers which their products is not greater than N

  • create semi-prime from them as a bit wise array of 0 and 1

  • create a prefix sum of the semi-primes

  • calculate the queries from P[i] to Q[i] in O(M)

The whole algorithm is of O(N * log(log(N)) + M) stated by the Codility’s test result evaluation.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement