xiaowei-system/test_primes.py

28 lines
569 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""测试 v2输出 1-100 间所有质数"""
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
limit = int(n**0.5) + 1
for i in range(3, limit, 2):
if n % i == 0:
return False
return True
def main():
primes = []
for n in range(1, 101):
if is_prime(n):
primes.append(n)
print(f"质数数量: {len(primes)}")
print(f"质数列表: {primes}")
return primes
if __name__ == "__main__":
main()