我的简单素数生成器有点问题
免责声明:我使用的数字非常大——现在超过2000000位,这可能会导致python的不稳定性,但我希望这是一个可以解决的已知问题
f = open('primes.txt', 'r') #this is a file containing all prime numbers less than n
#sequentially. I have another program that finds and adds
#more prime numbers to this file.
i = 1
for line in f: #Since the primes are separated by line breaks, I take the int of
#each line and multiply them together and stores the massive number
#in a variable called i
i = i*int(line)
f.close()
i = i+1 #since I know that every prime up to a certain point is a factor of
#i, I can add or subtract 1 to guarantee a number that has no prime
#factors (and therefore no factors)
print(i) #prints i to the terminal followed by 3 linebreaks
print("\n\n\n")
h = str(i) #I want to ultimately write i to a text file, but the write command
#doesn't take integer inputs
print(len(h)) #prints how large my new prime is
f = open('largestprime.txt', 'w') #opens a new file and overwrites what's currently inside
if int(h) == i: #I wrote this statement as a check when I started noticing something
#unusual was going on. It basically checks to make sure that the
#string h is the same thing as the integer i before writing to file.
f.write(h)
else:
f.write("Something Hinky's Going On")
input() #Waits for an enter before closing the terminal
这对我来说似乎很简单,但它打印到屏幕上的数字和写入文件的数字每次都不同。下面是最新的例子:
-终端打印:
6690896934...(2,009,810 other digits)...0355781011 (total of 2,009,830 digits)
-文件现在包含:
5562116702...(2,007,021 other digits)...6916126637 (total of 2,007,041 digits)
该文件缺少2000多个数字! 我是不是在写文件时出错了?在向文件中写入超长字符串时,python中是否存在已知的错误
任何帮助都将不胜感激。谢谢
目前没有回答
相关问题 更多 >
编程相关推荐