简体   繁体   English

试图找到素数的python代码。 代码计数不是素数。 找不到原因

[英]python code trying to find prime number. Code is counting not prime number. Can't find why

I am trying to find every prime number in a user chosen range, list them and count them.我试图在用户选择的范围内找到每个素数,列出它们并计算它们。 my code counts and list number that are not prime.我的代码计数和列表编号不是素数。 I really can't find why?我真的找不到原因? Could somebody please help me.有人可以帮我吗。

print("This code will count how many prime number exist in a certain range")
count = 0
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
prime = []
for num in range(lower, upper + 1):
    if num > 1:
        for i in range(2,num):
            if (num % i) == 0:
                break
            else:
                prime.append(num)
                break
print(prime)
print("There are", len(prime), "prime number between", lower, "and", upper)

Your primes part is wrong.你的素数部分是错误的。 I'm going to abstract that to a different method to make it easier.我将把它抽象为一种不同的方法,以使其更容易。 To be prime, a n%x must hold up for any x such that x is in the set of numbers [2,ceil(sqrt(n))] .要成为素数, n%x必须支持任何x ,使得x在数字集合[2,ceil(sqrt(n))]

Make sure you import math确保您import math

def is_prime(num):
    for i in range(2, math.ceil(num**(1/2))):
        if num%i==0:
            return False
    return True

Then, just replace然后,只需更换

if num > 1:
    for i in range(2,num):
        if (num % i) == 0:
            break
        else:
            prime.append(num)
            break

With,和,

if num>1:
   if is_prime(num):
      prime.append(num)

Your problem is that you dont wait all division checks to append the number to primes list:您的问题是您不等待所有除法检查将数字附加到素数列表:

            if (num % i) == 0:
                break
            else:
                prime.append(num)
                break

The fix for your code:您的代码的修复:

        is_prime =True
        for i in range(2,num):
            if num % i == 0:
                is_prime = False
                break

        if is_prime:
            prime.append(num)

$ python prime_test.py 
This code will count how many prime number exist in a certain range
Enter lower range: 12
Enter upper range: 20
[13, 17, 19]
('There are', 3, 'prime number between', 12, 'and', 20)

The problem is here,问题就在这里,

    for i in range(2,num):
        if (num % i) == 0:    #If num is divisible by SOME i, it is not prime. Correct.
            break
        else:                 #If num is not divisible by SOME i, it is not prime. WRONG.
            prime.append(num)
            break

You should have checked the condition like this : If num is not divisible by ANY i, it is prime .您应该像这样检查条件: If num is not divisible by ANY i, it is prime

With minimum changes using else with for,使用 else 和 for 进行最少的更改,

    for i in range(2,num):
        if (num % i) == 0:
            break
    else:
        prime.append(num)

Read more : Why does python use 'else' after for and while loops?阅读更多: 为什么python在for和while循环之后使用'else'?

Here is a solution to get the prime number form a list.这是从列表中获取素数的解决方案。 Ask user for range between the prime number should be created and store it in list :询问用户应创建质数之间的范围并将其存储在list

list4=list(range(3,10,1))  
prime_num=[x for x in list4 if x%2!=0 and x%3!=0]

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

相关问题 Python:找出一个数的两个质因数。 如何让这段代码更有效率? - Python: Find the two prime factors of a number. how to make this code more efficiency? 我试图找到某个数字以下的所有素数的总和。 如果初始数是素数,我们返回它 - I am trying to find the sum of all the prime numbers below a certain number. if the initial number is prime we return it 我有一个数字的素因子的Python列表。 我如何(pythonically)找到所有因素? - I have a Python list of the prime factors of a number. How do I (pythonically) find all the factors? 无法在python中找到素数代码中的错误 - Unable to find the mistake in prime number code in python 我似乎找不到 python 3 中数字的最高素数的正确代码 - I can't seem to find the corrrect code for the Highest prime factor of a number in python 3 Python素数代码 - Python prime number code 如何在不使用函数代码的情况下在python中找到第n个素数? - How to find nth prime number in python without using function code? 试图找到下一个质数 - Trying to find the next prime number python代码根据给定条件查找素数或合数 - python code to find prime or composite number according to given condition 找出第20,第30,第n个素数。 (我要20岁但不是30岁?)[Python] - Find out 20th, 30th, nth prime number. (I'm getting 20th but not 30th?) [Python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM