Finding number of integers which has exactly x divisors
Detailed guide and Python implementation for the 'Finding number of integers which has exactly x divisors' problem.
Problem Statement
Write a function count_with_x_divisors(n, x) that takes two positive integers n and x, and returns the count of integers in the range [1, n] (inclusive) that have exactly x divisors.
A divisor of a number k is any integer that divides k evenly. For example, divisors of 6 are 1, 2, 3, 6 (4 divisors).
- •1 <= n <= 1000
- •1 <= x <= 50
Examples
count_with_x_divisors(10, 2)
4
Numbers from 1 to 10 with exactly 2 divisors (i.e., prime numbers): 2, 3, 5, 7. That's 4 numbers.
count_with_x_divisors(10, 1)
1
Only the number 1 has exactly 1 divisor.
count_with_x_divisors(20, 4)
5
Numbers from 1-20 with exactly 4 divisors: 6(1,2,3,6), 8(1,2,4,8), 10(1,2,5,10), 14(1,2,7,14), 15(1,3,5,15). That's 5 numbers.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Regex
Master Regular Expressions (Regex) in Python. Learn to search, match, split, and substitute string data using the built-in re library.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python Regex Patterns (re module)
Reference guide for Python regular expressions. Learn match, search, findall, sub, and essential regex patterns.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.