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

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。