Python 基础知识简单

查找 1 到 100 之间的质数

“查找 1 到 100 之间的素数”问题的详细指南和 Python 实现。

问题陈述

简单

编写一个函数 primes_up_to_100() ,它不带任何参数,并返回 1 到 100 之间所有素数的列表(如果是素数,则包括 100)。

素数是大于 1 的数,除了 1 和它本身之外没有其他约数。前几个素数是 2, 3, 5, 7, 11, ...

约束条件
  • The range is fixed: 1 to 100
  • Return a list sorted in ascending order

示例

Example 1
Input
primes_up_to_100()
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Explanation

There are 25 prime numbers between 1 and 100. Each number in the list is only divisible by 1 and itself.

Example 2
Input
len(primes_up_to_100())
Output
25
Explanation

The total count of primes between 1 and 100 is 25.

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

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