[Python] Python Write to file[Python] Python Write to file
🧮
Primes
Week 8, 2026
//Paste your solution here if you want to share it publicly
def IsPrime(x: int):
for i in range(2, min(x, 10000)):
# for i in range(2, x):
if x % i == 0:
return False
return True
addCount = 0
minCount = 0
prevNum = 0
with open("example.txt", "w") as file:
for a in range(1, 10001):
if IsPrime(a ** 2 - 1) and a != 1:
file.write(str(a) + "^2 - 1 = " + str(a ** 2 - 1) + "\n")
file.write("Difference: " + str(a - prevNum) + "\n\n")
minCount += 1
prevNum = a
if IsPrime(a ** 2 + 1):
file.write(str(a) + "^2 + 1 = " + str(a ** 2 + 1) + "\n")
file.write("Difference: " + str(a - prevNum) + "\n\n")
addCount += 1
prevNum = a
file.write("--- Counts ---\n")
file.write("MinusOneCount: " + str(minCount) + "\n")
file.write("AddOneCount: " + str(addCount) + "\n")