Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
麻省理工学院公开课Python中查找第1000个素数的代码不起作用?_Python - Fatal编程技术网

麻省理工学院公开课Python中查找第1000个素数的代码不起作用?

麻省理工学院公开课Python中查找第1000个素数的代码不起作用?,python,Python,我刚刚开始编程,我的第一个个人任务是找到第1000个素数。我认为这段代码是正确的,但它列出了前1000多个数字。有什么见解吗 count = 1 num = 3 prime = [2] while count <= 1000: for x in range(2, num + 1): if num % x == 0 and num == x: prime.append(num) num += 2 count +

我刚刚开始编程,我的第一个个人任务是找到第1000个素数。我认为这段代码是正确的,但它列出了前1000多个数字。有什么见解吗

count = 1
num = 3
prime = [2]

while count <= 1000:
    for x in range(2, num + 1):
        if num % x == 0 and num == x:
            prime.append(num)
            num += 2
    count += 1

print(prime[1000])
count=1
num=3
素数=[2]
当计数
导入数学时
def是_prime(a):
对于x范围内的i(2,int(math.sqrt(a)+1)):
如果%i==0:
返回错误
返回真值
素数=[]
计数=2
而len(素数)<1000:
如果是_素数(计数):
素数。追加(计数)
计数+=1
打印(素数)`

我刚刚写了这个。它会问您用户想要看到多少素数,在本例中是1000。请随意使用它:)

#p是素数系列的序列号
#n是要测试的自然数序列,无论是否为素数
#i是自然数的序列,用于确定n进行测试
#L是用户希望看到的素数系列的序列极限
p=2;n=3
L=int(输入('输入要查看的质数:'))
打印(“#1素数是2”)

虽然(第7919页………..你怎么能说我认为这个代码是正确的,在同一句话中,它列出了前1000个奇数?你是第一次将数字添加到列表中,它们可以被任何数字整除,而你应该将它们添加到列表中,当它们不能被任何数字整除时,除了1和它们自己o当然,你需要在找到质数后去掉所有其他的倍数。请不要对不同的问题给出重复的答案。如果需要,请将问题标记为重复。
import math

def is_prime(a):
    for i in xrange(2, int(math.sqrt(a)+1)):
        if a%i == 0:
            return False
    return True

primes=[]
count = 2
while len(primes) < 1000:
    if is_prime(count):
        primes.append(count)
    count += 1

print(primes)`
# p is the sequence number of prime series
# n is the sequence of natural numbers to be tested if prime or not
# i is the sequence of natural numbers which will be used to devide n for testing
# L is the sequence limit of prime series user wants to see
p=2;n=3
L=int(input('Enter the how many prime numbers you want to see: '))
print ('# 1  prime is 2')
while(p<=L):
    i=2
    while i<n:
        if n%i==0:break
        i+=1
    else:print('#',p,' prime is',n); p+=1
    n+=1 #Line X

#when it breaks it doesn't execute the else and goes to the line 'X'