CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day11/file3.py
Views: 729
1
"""
2
写文本文件
3
将100以内的素数写入到文件中
4
5
Version: 0.1
6
Author: 骆昊
7
Date: 2018-03-13
8
"""
9
10
from math import sqrt
11
12
13
def is_prime(n):
14
for factor in range(2, int(sqrt(n)) + 1):
15
if n % factor == 0:
16
return False
17
return True
18
19
20
# 试一试有什么不一样
21
# with open('prime.txt', 'a') as f:
22
with open('prime.txt', 'w') as f:
23
for num in range(2, 100):
24
if is_prime(num):
25
f.write(str(num) + '\n')
26
print('写入完成!')
27
28