Python 基础知识简单

查找正好有 x 个约数的整数的个数

“查找恰好具有 x 个除数的整数的数量”问题的详细指南和 Python 实现。

问题陈述

简单

编写一个函数 count_with_x_divisors(n, x),它接受两个正整数 nx,并返回 [1, n](含)范围内恰好具有 x 除数的整数的计数。

数字 k 的除数是整除 k 的任何整数。例如,6 的约数为 1、2、3、6(4 个约数)。

约束条件
  • 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?
考虑使用特定于数字的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。