如何修复 python n 生成的素数列表的最后一行被截断?
How to fix the last row of python n generated primelist getting cut off?
n 个生成的素数列表的最后一行被奇数输入截断
def isPrime(m, L):
while i < n:
if n % i == 0:
return False
i = 1
return True
print()
n = int(input('Enter the number of Primes to compute: '))
primeList = []
x = 2
while len(primeList) < n:
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
x += 1
print()
n = str(n)
print('The first ' + n + ' primes are: ')
count = 0
tempstring = []
last_row = len(primeList) / 10 + (1 if len(primeList) % 10 else 0)
row = 0
for i in range(0, len(primeList)):
tempstring.append(str(primeList[i]))
count += 1
if count == 10:
print(' '.join(tempstring))
count = 0
tempstring = []
row += 1
elif count == (len(primeList) % 10) and row == (last_row - 1):
print(' '.join(tempstring))
# done
print()
current output:
Enter the number of Primes to compute: 97
The first 97 primes are:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
missing the last line: 467 479 487 491 499 503 509
如何让它打印最后一行素数?
您的代码实际上在 python 2 中有效,但在 python 3 中无效。
在python2、这一行
last_row = len(primeList) / 10 + (1 if len(primeList) % 10 else 0)
会给你10
但在 python 3 中,它将是 10.7
。
当然,如果是10.7
,你的条件elif count == (len(primeList) % 10) and row == (last_row - 1):
根本就不符合
要修复它,只需执行
last_row = int(len(primeList) / 10) + (1 if len(primeList) % 10 else 0)
即确保 len(primeList) / 10
的结果是一个整数。
更好的是,您应该简单地将 primeList
中的数字划分为一个包含 10 个元素的列表。然后你就可以去掉这些复杂的逻辑了。有关详细信息,请参阅 How do you split a list into evenly sized chunks?
n 个生成的素数列表的最后一行被奇数输入截断
def isPrime(m, L):
while i < n:
if n % i == 0:
return False
i = 1
return True
print()
n = int(input('Enter the number of Primes to compute: '))
primeList = []
x = 2
while len(primeList) < n:
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
x += 1
print()
n = str(n)
print('The first ' + n + ' primes are: ')
count = 0
tempstring = []
last_row = len(primeList) / 10 + (1 if len(primeList) % 10 else 0)
row = 0
for i in range(0, len(primeList)):
tempstring.append(str(primeList[i]))
count += 1
if count == 10:
print(' '.join(tempstring))
count = 0
tempstring = []
row += 1
elif count == (len(primeList) % 10) and row == (last_row - 1):
print(' '.join(tempstring))
# done
print()
current output:
Enter the number of Primes to compute: 97
The first 97 primes are:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
missing the last line: 467 479 487 491 499 503 509
如何让它打印最后一行素数?
您的代码实际上在 python 2 中有效,但在 python 3 中无效。
在python2、这一行
last_row = len(primeList) / 10 + (1 if len(primeList) % 10 else 0)
会给你10
但在 python 3 中,它将是 10.7
。
当然,如果是10.7
,你的条件elif count == (len(primeList) % 10) and row == (last_row - 1):
根本就不符合
要修复它,只需执行
last_row = int(len(primeList) / 10) + (1 if len(primeList) % 10 else 0)
即确保 len(primeList) / 10
的结果是一个整数。
更好的是,您应该简单地将 primeList
中的数字划分为一个包含 10 个元素的列表。然后你就可以去掉这些复杂的逻辑了。有关详细信息,请参阅 How do you split a list into evenly sized chunks?