Python BasicsЛегко

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.

Постановка задачи

Легко

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

Примеры

Example 1
Input
count_with_x_divisors(10, 2)
Output
4
Explanation

Numbers from 1 to 10 with exactly 2 divisors (i.e., prime numbers): 2, 3, 5, 7. That's 4 numbers.

Example 2
Input
count_with_x_divisors(10, 1)
Output
1
Explanation

Only the number 1 has exactly 1 divisor.

Example 3
Input
count_with_x_divisors(20, 4)
Output
5
Explanation

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?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Открыть в редакторе
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Рекомендуемые ресурсы Python

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Python Regex

Освойте регулярные выражения (Regex) в Python. Научитесь искать, сопоставлять, разбивать и заменять строковые данные с помощью встроенной библиотеки re.

Посмотреть ресурс
Практическое руководство

Как найти длину списка в Python

Узнайте, как найти длину списка в Python с помощью функции len(). Поймите временную сложность O(1) и количество проверок.

Посмотреть ресурс
Шпаргалка

Шаблоны регулярных выражений Python (перемодуль) Шпаргалка

Справочное руководство по регулярным выражениям Python. Изучите шаблоны сопоставления, поиска, поиска, подзаголовков и основные шаблоны регулярных выражений.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс