0
x=0
y = raw_input("""Up to what number would you like to locate primes?: """)
for i in range(int(y)):
x = x + 1
if x%2 and x%3 and x%5 and x%7:
print x, '--> PRIME'
elif x==2 or x==3 or x==5 or x==7:
print x, '--> PRIME'
elif x==1:
print x
else:
print x

过去一周半我一直在休息,我想我会花时间学习 Python。我今晚早些时候写了这个小脚本,它简单地打印出用户指定范围内的整数列表并识别其中哪些是素数。我想做但似乎找不到任何文档是让脚本计算素数的出现并回显“在您指定的间隔内有(空白)素数。” 我不知道在哪里寻找那种东西。我不想要答案,但我希望有人指出我需要前进的方向。

一如既往,谢谢。

PS这确实有效,我只是想让它“更好”。

4

3 回答 3

1

I feel compelled to point out that your code works as long as you enter a number less than 11^2 = 121 (it would mistakenly identify 121 as prime).

The way you've written it is OK, but it doesn't really take advantage of the stronger features of Python. Let's look at the code:

x=0
y = raw_input("""Up to what number would you like to locate primes?: """)
for i in range(int(y)):
    x = x + 1
    if x%2 and x%3 and x%5 and x%7:
        print x, '--> PRIME'
    elif x==2 or x==3 or x==5 or x==7:
        print x, '--> PRIME'
    elif x==1:
        print x
    else:
        print x

In Python, for i in range(int(y)) will set i to 0, 1, 2, ..., y-1. So, you don't need a separate x variable to keep track of the value:

y = raw_input("""Up to what number would you like to locate primes?: """)
for x in range(int(y)):
    if x%2 and x%3 and x%5 and x%7:
        print x, '--> PRIME'

In Python, you can use in to quickly test equality against several choices (a special case of testing container membership), so x==2 or x==3 or x==5 or x==7 can be rewritten as x in (2, 3, 5, 7).

Now, if you want to count the number of primes, let's add a counter:

num_primes = 0
y = raw_input("""Up to what number would you like to locate primes?: """)
for x in range(int(y)):
    if x == 1:
        # 1 actually passes the modulo tests and would be considered prime in the original code
        print x
    elif (x%2 and x%3 and x%5 and x%7) or x in (2, 3, 5, 7):
        num_primes += 1 # this increments num_primes by 1
        print x, '--> PRIME'
    else:
        print x

And finally print it out with a nice formatted message:

print 'There are {} primes between 0 and {}'.format(num_primes, y)

Ta-da!

于 2012-09-19T01:30:14.850 回答
0

人们确定整数是否为素数的一般方法是循环遍历每个整数从 1 到nn所讨论的整数在哪里)并计算因数(记住这inif的因数n % i == 0)。当然,他们然后测试这个计数是否等于 2;如果是,n则为素数 - 如果不是,n则不是素数。像这样的东西(在伪代码中):


为从 1 到 n 的每个 i设置 count = 0 { if (n % i is 0) then increment "count"}
if ("count" is 2) then "n" is prime

显然有更好的方法,但对于刚刚学习的人来说应该足够了。在您的情况下,您希望将此过程应用于整个范围内的每个数字,计算遇到的素数数量并打印结果。

就像一个注释:确保正确缩进你的代码,作为提到的注释之一(这是 Python 必须的)。

于 2012-09-19T00:47:33.243 回答
0

据我了解,您对您的算法感到满意(即使您显然知道它并不正确);您只是在询问要添加什么以使其跟踪您在此过程中打印出“--> PRIME”的次数,对吗?

嗯,描述是一个线索。您需要一个计数器变量,它从 0 开始,然后每次打印出“--> PRIME”时将该计数器加 1。然后你可以在最后打印那个计数器:

x=0
y = raw_input("""Up to what number would you like to locate primes?: """)
primes = 0
for i in range(int(y)):
  x = x + 1
  if x%2 and x%3 and x%5 and x%7:
    print x, '--> PRIME'
    primes += 1
  elif x==2 or x==3 or x==5 or x==7:
    print x, '--> PRIME'
    primes += 1
  elif x==1:
    print x
  else:
    print x
print 'There were', primes, 'within your specified interval'

您可能要研究的下一件事是编写一个为您进行检查的函数;然后如果函数返回 True,则增加计数器并打印数字。

然后,您可以更改代码以将 range(int(y)) 过滤到一个新列表中,其中仅包含函数返回 true 的那些值;一旦你有了它,你只需要计算结果中有多少元素。

然后您可以弄清楚如何将其编写为列表推导式(最初掌握窍门有点棘手,一旦掌握它会使您的代码变得更加简单)。

然后看看将原始显式列表生成器转换为生成器函数,和/或将列表推导转换为生成器表达式。这允许您在结果进入时打印出来(通过迭代生成器),但这意味着您不能再仅打印长度来获得总数,因此您需要一个不同的解决方案。

于 2012-09-19T00:51:44.507 回答