Find the Count of Numbers Which Are Not Special - Problem

You are given 2 positive integers l and r.

For any number x, all positive divisors of x except x are called the proper divisors of x.

A number is called special if it has exactly 2 proper divisors.

For example:

  • The number 4 is special because it has proper divisors 1 and 2.
  • The number 6 is not special because it has proper divisors 1, 2, and 3.

Return the count of numbers in the range [l, r] that are not special.

Input & Output

Example 1 — Basic Range
$ Input: l = 5, r = 7
Output: 3
💡 Note: Numbers 5, 6, 7: None are special (5 has 1 proper divisor, 6 has 3 proper divisors, 7 has 1 proper divisor), so all 3 are not special
Example 2 — Including Special Number
$ Input: l = 4, r = 16
Output: 11
💡 Note: Range has 13 numbers total. Special numbers: 4 (divisors 1,2), 9 (divisors 1,3), 16 (divisors 1,2,4,8) - wait, 16 has 4 proper divisors, only 4 and 9 are special. Non-special count: 13 - 2 = 11
Example 3 — No Special Numbers
$ Input: l = 10, r = 15
Output: 6
💡 Note: Numbers 10,11,12,13,14,15: None are squares of primes, so all 6 numbers are not special

Constraints

  • 1 ≤ l ≤ r ≤ 109

Visualization

Tap to expand
Find Count of Non-Special Numbers INPUT 5 6 7 l=5 r=7 l = 5, r = 7 Range: [5, 6, 7] Special Number: Has exactly 2 proper divisors (excl. itself) Example: 4 is special: divisors 1,2 6 is NOT: divisors 1,2,3 ALGORITHM STEPS 1 Analyze each number Count proper divisors 2 Check n=5 Divisors: 1 (only 1) 3 Check n=6 Divisors: 1,2,3 (3 divs) 4 Check n=7 Divisors: 1 (only 1) Divisor Analysis Num Proper Div Special? 5 {1} NO 6 {1,2,3} NO 7 {1} NO FINAL RESULT Numbers in range [5,7]: 5 Not Special 6 Not Special 7 Not Special Count of non-special: Output: 3 Verification: All 3 numbers have != 2 proper divisors Key Insight: A number is special if it has exactly 2 proper divisors. This means the number must be p^2 (prime squared). For example: 4=2^2, 9=3^2, 25=5^2 are special. Optimized approach: find primes p where l <= p^2 <= r, then subtract from total count. In [5,7]: no prime squares exist, so all 3 numbers are NOT special. TutorialsPoint - Find the Count of Numbers Which Are Not Special | Optimized Divisor Counting
Asked in
Google 25 Microsoft 18
30.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen