Recently Chef become very much interested in perfect squares. We all know Chef and his…
- User
Recently Chef become very much interested in perfect squares. We all know Chef and his weird interests. Anyways Chef will be soon writing his masters thesis on perfect squares revealing what-not-known properties of perfect squares. While doing his research, he happened to be confronted with some interesting perfect squares. These prefect squares consists only of digits which are themselves perfect squares. 0, 1, 4 and 9 are such digits. These are called perfect digits. As we all know Chef also has habit of asking too many questions, he is asking- given two numbers a and b, how many perfect squares exists between these two numbers inclusive, that contains only perfect digits. Input: First line of input will contains T, number of test cases. Then T lines follows, each containing two positive integers a and b. Constraints: T <= 500 1<= a <= b <= 10000000000 Output: For each input, output number of perfect digit squares between given numbers. Sample Input: 2 1 10 100 10000 Output: 3 9
Assistant
pythonimport bisect valid_squares = [] for i in range(0, 100001): square = i * i s = str(square) valid = True for c in s: if c not in {'0', '1', '4', '9'}: valid = False break if valid: valid_squares.append(square) T = int(input()) for _ in range(T): a, b = map(int, input().split()) left = bisect.bisect_left(valid_squares, a) right = bisect.bisect_right(valid_squares, b) print(right - left)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.