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 max_k = int(math.isqrt(10**9)) # Precompute up to square root of maximum possible Y # Sieve to compute smallest prime factors (SPF) spf = list(range(max_k + 1)) for i in range(2, int(math.isqrt(max_k)) + 1): if spf[i] == i: # i is a prime for j in range(i * i, max_k + 1, i): if spf[j] == j: spf[j] = i # Compute Mobius function for each number up to max_k mobius = [0] * (max_k + 1) mobius[1] = 1 for i in range(2, max_k + 1): x = i primes = set() is_square_free = True while x != 1 and is_square_free: p = spf[x] if p in primes: is_square_free = False break primes.add(p) x = x // p if x % p == 0: is_square_free = False if is_square_free: mobius[i] = (-1) ** len(primes) else: mobius[i] = 0 def count_square_free(n): if n == 0: return 0 res = 0 max_k_val = int(math.isqrt(n)) for k in range(1, max_k_val + 1): mu = mobius[k] if mu == 0: continue res += mu * (n // (k * k)) return res t = int(input()) for _ in range(t): x, y = map(int, input().split()) print(count_square_free(y) - count_square_free(x - 1))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.