How to gracefully judge whether a number is a prime in python?

how do the great gods check whether they are prime numbers? Is there a ready-made module to call?

May.25,2022

look at the answer "

is a similar problem before


elegance is no way to be elegant, only to improve performance as much as possible

import math

def is_prime(number):
    if number > 1:
        if number == 2:
            return True
        if number % 2 == 0:
            return False
        for i in range(3, int(math.sqrt(number) + 1), 2):
            if number % i == 0:
                return False
        return True
    return False

https://www.zhihu.com/questio...
depends on your actual needs. How much prime number do you want to check?
if any large number is judged to be a prime number, the first edition of engineering practice uses a fast check (it is not guaranteed to be a prime number, but the speed is very fast, and the probability that it is not a prime number is very small. This kind of judging prime number can be used in some practice, but can not be regarded as prime number directly in mathematics)
some specific large numbers have specific methods to judge whether they are prime numbers, but this method can only be aimed at specific numbers, not universal
if the numbers are not enough to be hard calculated, the speed will be relatively slow but 100% accurate

Menu