Game of numbers is a game where you are given two integers (X and Y), and you have to…
- User
Game of numbers is a game where you are given two integers (X and Y), and you have to print the number of special numbers between X and Y both inclusive.
The property of a special numbers is as follows:
A special number is not divisible by any number of the form ZZ* where (Z; >; 1). Input: T, the number of test cases. Each test case consists of two space separated integers denoting X and Y.Output: The required answer in one line for each test case.
Constraints:
1 ≤ T ≤ 10
1 ≤ X,Y ≤ 10 ^ 9 0 ≤ |X-Y| ≤ 10^6SAMPLE INPUT 1 1 10
SAMPLE OUTPUT 7
Explanation
In given test case, 4,8 and 9 are not special.
Assistant
pythonimport math def sieve(n): sieve = [True] * (n + 1) sieve[0] = sieve[1] = False for i in range(2, int(math.sqrt(n)) + 1): if sieve[i]: sieve[i*i : n+1 : i] = [False] * len(sieve[i*i : n+1 : i]) primes = [i for i, is_prime in enumerate(sieve) if is_prime] return primes # Precompute primes up to sqrt(1e9) which is approximately 31623 max_prime = int(math.sqrt(10**9)) + 1 primes = sieve(max_prime) def count_special_numbers(X, Y): size = Y - X + 1 sieve = [True] * size for p in primes: p_squared = p * p if p_squared > Y: break # Find the first multiple of p_squared >= X first = ((X + p_squared - 1) // p_squared) * p_squared if first > Y: continue # Mark all multiples of p_squared in the range [X, Y] for m in range(first, Y + 1, p_squared): sieve[m - X] = False return sum(sieve) T = int(input()) for _ in range(T): X, Y = map(int, input().split()) print(count_special_numbers(X, Y))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.