On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As…
- User
On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as:
t = a2 + b2, where a, b are arbitrary positive integers.
Now, the boys decided to find out how many days of the interval [l, r] (l ≤ r) are suitable for pair programming. They decided that the day i (l ≤ i ≤ r) is suitable for pair programming if and only if the number i is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days. Input
The first line of the input contains integer numbers l, r (1 ≤ l, r ≤ 3·108).
Output
In the only line print the number of days on the segment [l, r], which are lucky for Peter and Bob at the same time.
Examples
Input
3 5
Output
1
Input
6 66
Output
7
Assistant
import numpy as np def get_primes_up_to(n): sieve = np.ones(n + 1, dtype=bool) sieve[0] = sieve[1] = False for i in range(2, int(n**0.5) + 1): if sieve[i]: sieve[i*i::i] = False primes = np.nonzero(sieve)[0] return primes def count_lucky_days(l, r): if l > r: return 0 sqrt_r = int(r**0.5) primes_fundamental = get_primes_up_to(sqrt_r) size = r - l + 1 sieve_range = np.ones(size, dtype=bool) for p in primes_fundamental: if p == 0 or p == 1: continue # Compute first multiple >= l first_multiple_val = max(p * p, ((l + p - 1) // p) * p) if first_multiple_val > r: continue first_pos = first_multiple_val - l # Mark multiples from first_pos with steps of p sieve_range[first_pos::p] = False if l == 1: sieve_range[0] = False # 1 is not a prime primes_in_range = np.add(np.where(sieve_range)[0], l) # Calculate count where primes are 2 or congruent to 1 mod4 condition = (primes_in_range == 2) | (primes_in_range % 4 == 1) return np.sum(condition) def main(): import sys l, r = map(int, sys.stdin.readline().split()) print(count_lucky_days(l, r)) 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.