This question was asked today in TNQT Digital 2 Exam
Find how many Sexy Prime Numbers in a given range n and m.
Constraint: 2 <= n < m <= 1000,000,000.
I think it’s a pattern question.If u see,each pair can be represented as (6n+1,6(n+1)+1) or (6n-1,6(n+1)-1).But I failed to notice this during test&tried Sieve which obviously gave TLE.
used vector of bool of size n+1
found all primes <= n (n given in input) using seive O(n*log(log(n)))
incremented answer for all numbers in range m to n-6 such that both m and m+6 are prime.
This passed all 7 Test cases but when I manually gave m=2, n=10^9 it timed out.
I tried using sqrt method but it said “Process killed after 2.0000 seconds.”
If C and C++ has 2 sec time limit, shouldn’t python have 10 sec?
If they’d have mentioned it, I’d have started with C++. Didn’t have enough time to do it over in C++.
Actually it is already widely known that except 2 and 3, all prime numbers are either (6N-1) or (6N+1). I never came across this so could not solve it under given time constraint.