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 sys import math def sieve(n): is_prime = [True] * (n + 1) is_prime[0], is_prime[1] = False, False for i in range(2, int(math.isqrt(n)) + 1): if is_prime[i]: for j in range(i * i, n + 1, i): is_prime[j] = False return [i for i, val in enumerate(is_prime) if val] # Precompute primes up to sqrt(1e9) ≈ 31622 primes = sieve(31622) def count_square_free(X, Y): if X > Y: X, Y = Y, X size = Y - X + 1 is_square_free = [True] * size for p in primes: p_squared = p * p if p_squared > Y: break start = ((X + p_squared - 1) // p_squared) * p_squared if start > Y: continue for m in range(start, Y + 1, p_squared): idx = m - X if 0 <= idx < size: is_square_free[idx] = False return sum(is_square_free) def main(): input = sys.stdin.read().split() T = int(input[0]) ptr = 1 for _ in range(T): X = int(input[ptr]) Y = int(input[ptr + 1]) ptr += 2 print(count_square_free(X, Y)) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.