[英]ValueError: invalid literal for int() with base 10: '59574966\n9'
I'm trying to do a google challenge: "find the first 10-digit prime in consecutive digits of e".我正在尝试进行谷歌挑战:“在 e 的连续数字中找到第一个 10 位素数”。
I get the error:" ValueError: invalid literal for int() with base 10: '59574966\\n9'"我收到错误:“ValueError:int() 的无效文字以 10 为基数:'59574966\\n9'”
This is the relevant code:这是相关代码:
def getNumber(i, text):
string = ""
for x in range(i,i+10):
string += text[x]
return string
def init(text):
i=0
while(i<2000000-10):
number = getNumber(i,text)
if(isPrime(int(number))):
print(number)
break;
i+=1
file = open("C://Users/Name/Desktop/e.txt", 'r')
e = file.read()
init(e)
Im loading a file with e to 2 million decimals.我正在加载一个带有 e 到 200 万位小数的文件。 isPrime() is a function that I am sure works.
isPrime() 是一个我确信有效的函数。
Instead of doing this:而不是这样做:
file = open("C://Users/Name/Desktop/e.txt", 'r')
e = file.read()
init(e)
You should perhaps do this:你或许应该这样做:
with open("C://Users/Name/Desktop/e.txt", 'r') as fd:
for e in fd:
e = e.strip()
if e:
init(e)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.