該程序用於計算素數並將其保存到文件中。保存功能尚未正確編程,請忽略。該程序通過比較奇數與以前的素數進行工作。如果它不是這些數字的倍數,那麼它就是素數。從理論上講它應該然而,工作中當我嘗試通過從列表中返回的錯誤信息的質數來劃分數:錯誤消息:不支持的操作數類型___
Traceback (most recent call last): File "C:\Users\Archie\Desktop\maths python\prime\prime v1.3.py", line 51, in primeCheck(num) File "C:\Users\Archie\Desktop\maths python\prime\prime v1.3.py", line 8, in primeCheck check = int(num)/listImport TypeError: unsupported operand type(s) for /: 'int' and 'list'
能否請你無論是建議我該如何解決這個問題,或者提出一個不同的方法解決問題。
def primeCheck(num):
divider = 2
refresh = 0
firstPoint = 0
secondPoint = 1
while refresh == 0:
listImport = primeList[firstPoint:secondPoint]
check = int(num)/listImport
if (check.is_integer()):
refresh = 1
else:
firstPoint = firstPoint + 1
secondPoint = secondPoint + 1
if secondPoint > len(primeList):
file.write(str(num) + "\n")
print(str(num))
global x
x = x + 1
refresh = 1
primeList.append
\\ if (int(num)/divider).is_integer():
\\ if divider == num:
\\ file.write(str(num) + "\n")
\\ print(str(num))
\\ global x
\\ x = x + 1
\\ refresh = 1
\\ else:
\\ refresh = 1
\\ else:
\\ divider = divider + 1
global file
repeat = input("How many numbers do you want to add to the existing file?\n")
file = open("Prime results v1.3.txt", "r")
global x
x = 1
num = file.readline()
file.close()
global file
file = open("Prime results v1.3.txt", "a")
num = int(num)
global primeList
primeList = [2]
while x <= int(repeat):
primeCheck(num)
num = num + 2
file.close()
該地區雙刀削減是我嘗試過,以前的方法工作,但這種方式更有效。