简体   繁体   English

你如何在python中找到前N个素数?

[英]How do you find the first N prime numbers in python?

I am pretty new to python, so I don't fully understand how to use loops.我对 python 很陌生,所以我不完全理解如何使用循环。 I am currently working on a piece of code that I have to find the first N prime numbers.我目前正在编写一段代码,我必须找到前 N 个素数。 The result that is desired is if you input 5, it outputs 2, 3, 5, 7, and 11, but no matter what I input for 'max', the output always ends up being 2 and 3. Is there a way to improve this?所需的结果是,如果您输入 5,它会输出 2、3、5、7 和 11,但无论我为“max”输入什么,输出总是最终为 2 和 3。有没有办法改善这个?

max=int(input("How many prime numbers do you want: "))
min=2
while(min<=(max)):
  for c in range(2, min):
    if min%c==0:
      break
  else:
    print min
    min=min+1

You only increment min in the else block, ie, if min % c is nonzero for all c , ie, if min is prime.您只在else块中增加min ,即,如果min % c对于所有c不为零,即,如果min是素数。 This means that the code won't be able to move past any composite numbers.这意味着代码将无法越过任何合数。 You can fix this by unindenting min=min+1 one level so that it lines up with the for and else .您可以通过将min=min+1取消缩进一级来解决此问题,使其与forelse

number = int(input("Prime numbers between 2 and "))
for num in range(2,number + 1):
       if num > 1:
            for i in range(2,num):
                if (num % i) == 0:
                     break
            else:
                print(num)

I guess this will help, let me know. 我想这会有所帮助,让我知道。

number = int(input("How many prime numbers do you want ? "))

cnt = 0
for i in range (1, number+1):
    for j in range(1, i+1):
        if(i%j == 0):
            cnt += 1
    if(cnt == 2):
        print(i)
    cnt = 0

Solution: Get the nth prime number entry.解决方案:获取第 n 个质数条目。 Iterate through each natural numbers for prime number and append the prime number to a list.遍历每个自然数以获得质数并将质数附加到列表中。 Terminate the program when length of a list satisfies the user nth prime number entry.当列表的长度满足用户第 n 个素数条目时终止程序。

# Get the number of prime numbers entry.
try:
    enterNumber = int(input("List of nth prime numbers: "))
except:
    print("The entry MUST be an integer.")
    exit()
startNumber = 1
primeList = []
while True:
    # Check for the entry to greater than zero.
    if enterNumber <= 0:
        print("The entry MUST be greater than zero.")
        break
    # Check each number from 1 for prime unless prime number entry is satisfied.
    if startNumber > 1:
        for i in range(2,startNumber):
            if (startNumber % i) == 0:
                break
        else:
            primeList.append(startNumber)
    if (len(primeList) == enterNumber):
        print(primeList)
        break
    else:
        startNumber = startNumber + 1
        continue

The following code will give you prime numbers between 3 to N, where N is the input from user:以下代码将为您提供 3 到 N 之间的素数,其中 N 是用户的输入:

number = int(input("Prime numbers between 2, 3 and ")) 
for i in range(2,number):
    for j in range(2,int(i/2)+1):
        if i%j==0:
    break
        else:
            if j==int(i/2):
                print(i)

You can see to check a number i to be prime you only have to check its divisibility with numbers till n/2.你可以看到检查一个数字 i 是素数,你只需要检查它与数字的整除性,直到 n/2。

Try that :试试看:

n = int(input("First N prime number, N ? "))
p = [2]
c = 2

while len(p) < n:
    j = 0
    c += 1
    while j < len(p):
        if c % p[j] == 0:
            break
        elif j == len(p) - 1:
            p.append(c)
        j += 1
print(p)

Its simple.这很简单。 Check the below code, am sure it works!检查下面的代码,确保它有效!

N = int(input('Enter the number: ')
i=1
count=0
while(count<N):
      for x in range(i,i+1):
          c=0
          for y in range(1,x+1):
              if(x%y==0):
                 c=c+1
          if(c==2):
             print(x)
             count=count+1
      i=i+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM