Python 显示从 1 到 100 的所有质数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15963707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:28:42  来源:igfitidea点击:

Python displays all of the prime numbers from 1 through 100

python

提问by user2210599

I'm trying to print the all of the prime numbers from 1 through 100 by using Boolean function.

我正在尝试使用布尔函数打印从 1 到 100 的所有素数。

Below is my code that is working.

下面是我正在运行的代码。

for n in range(1,101):
status = True
if n < 2:
    status = False
else:
    for i in range(2,n):
        if n % i == 0:
            status = False

    if status:
        print(n, '', sep=',', end='')

But when I put the code in the function and run module, there's nothing print on the shell. What did I do wrong?

但是当我将代码放入函数并运行模块时,shell 上没有任何打印内容。我做错了什么?

is_prime():
    for n in range(1,101):
        status = True
        if n < 2:
            status = False
        else:
            for i in range(2,n):
                if n % i == 0:
                    status = False
        return status

if is_prime():    
    print(n, '', sep=',', end='')

Below is the output of the program. How do I prevent the last comma from printing?
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,

下面是程序的输出。如何防止打印最后一个逗号?
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,

采纳答案by Mahdi-bagvand

try this

尝试这个

def is_prime(n):
    status = True
    if n < 2:
        status = False
    else:
        for i in range(2,n):
            if n % i == 0:
                status = False
    return status

for n in range(1,101):
    if is_prime(n):
        if n==97:
            print n
        else:
            print n,",",

outputis
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

output
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

回答by user2589636

How about this it accomplishes the same thing but instead asks the user for the input:

怎么样,它完成了同样的事情,而是要求用户输入:

num1 = input("Input a number: ")
num2 = input("Input another number: ")


for x in range(num1,num2):
    prime = True
    for i in range(2,x):
        if (x%i==0):
            prime = False
    if prime == True:
       print x

print "Done......"

And if you want to just solve for the numbers you input your self then take out this part:

如果你只想解决你自己输入的数字,那么去掉这部分:

num1 = input("Input a number: ")
num2 = input("Input another number: ")

And change the range from num1,num2 too 1 and 100 for example:

并将范围从 num1,num2 更改为 1 和 100,例如:

for x in range(1,100):

回答by Hymantrader

I think your best solution would be to append an array, which fixes the comma issue and is faster/more optimized. And then if needed you could strip out the values, store them to file, whathaveyou. Good luck!

我认为你最好的解决方案是附加一个数组,它解决了逗号问题并且更快/更优化。然后,如果需要,您可以删除这些值,将它们存储到文件中,什么都有。祝你好运!

def get_primes(start, end):
    out = list()
    if start <= 1:
        start = 2

    sieve = [True] * (end + 1)
    for p in range(start, end + 1):
        if (sieve[p]):
            out.append(p)
            for i in range(p, end + 1, p):
                sieve[i] = False
    return out

print(get_primes(1, 100))

Output:

输出:

[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]

Please see my comment and others on stack overflow #11619942 "print series of prime numbers in python"

请参阅我对堆栈溢出的评论和其他评论 #11619942“在 python 中打印质数系列

回答by Kuldeep Choughule

Simple way to display range of prime Number #

显示质数范围的简单方法#

  # Program to display prime number till n nubers

def prime(number):
    for num in range(2,number):
        status = True
        for i in range(2,num):
            if num % i == 0:
                status = False
        if status:
            print(num)


prime(101)

print "Program Ends here"

回答by iku

n=int(input())
for i in range(1,int(n)):
    for j in range(2,(i+1)):
        if i%j==0:
            if i==j:
                print(i)
            break

This is the short way ...

这是捷径...

回答by Wheat

A couple of different ways to do it

几种不同的方法来做到这一点

primes = [x for x in range(2,100) if(all(x % j for j in range(2, x)))]

primes = []
for x in range(2, 101):
    if(False not in (x % j for j in range(2, x))):
        primes.append(x)

primes = []
for x in range(2, 101):
    flag = True
    for j in range(2, x):
        if(not x % j):
            flag = False
    if(flag):
        primes.append(x)

print(primes)

打印(素数)

回答by Aditya Kumar Singh

Very simple way to do it as follows:

非常简单的方法如下:

def prime(n):
    p = True
    for i in range(2,n):
        if (n%i == 0):
            p = False
    return p

for j in range(2,201):
    if prime(j):
        print (j)

回答by senel34

Here's an example where the number is only checked against prime numbers since all non prime numbers are divisible by a prime number (linkto related question in math stackexchange)

这是一个示例,其中仅针对素数检查数字,因为所有非素数都可以被素数整除(链接到数学堆栈交换中的相关问题)

prime_nums = []

for num in range(2,101):
    isPrime = True
    for prime in prime_nums:
        isPrime = not (num % prime == 0)
        if prime * 2 >= num or not isPrime:
            break
    if isPrime:
        prime_nums.append(num)
print(prime_nums)

回答by Xinxin Zhang

def ex1():
    count = 3

    while count < 101:
        isPrime = True

        for i in range(2, int(math.sqrt(count))+1):
            if count % i == 0:
                isPrime = False
        if isPrime:
            print(count, end=' ')
        count += 1
ex1()