How to create the most compact mapping n → isprime(n) up to a limit N?

后端 未结 30 2748
遇见更好的自我
遇见更好的自我 2020-11-22 02:11

Naturally, for bool isprime(number) there would be a data structure I could query.
I define the best algorithm, to be the algorithm that pr

相关标签:
30条回答
  • 2020-11-22 02:47

    There are many ways to do the primality test.

    There isn't really a data structure for you to query. If you have lots of numbers to test, you should probably run a probabilistic test since those are faster, and then follow it up with a deterministic test to make sure the number is prime.

    You should know that the math behind the fastest algorithms is not for the faint of heart.

    0 讨论(0)
  • 2020-11-22 02:48

    The fastest algorithm for general prime testing is AKS. The Wikipedia article describes it at lengths and links to the original paper.

    If you want to find big numbers, look into primes that have special forms like Mersenne primes.

    The algorithm I usually implement (easy to understand and code) is as follows (in Python):

    def isprime(n):
        """Returns True if n is prime."""
        if n == 2:
            return True
        if n == 3:
            return True
        if n % 2 == 0:
            return False
        if n % 3 == 0:
            return False
    
        i = 5
        w = 2
    
        while i * i <= n:
            if n % i == 0:
                return False
    
            i += w
            w = 6 - w
    
        return True
    

    It's a variant of the classic O(sqrt(N)) algorithm. It uses the fact that a prime (except 2 and 3) is of form 6k - 1 or 6k + 1 and looks only at divisors of this form.

    Sometimes, If I really want speed and the range is limited, I implement a pseudo-prime test based on Fermat's little theorem. If I really want more speed (i.e. avoid O(sqrt(N)) algorithm altogether), I precompute the false positives (see Carmichael numbers) and do a binary search. This is by far the fastest test I've ever implemented, the only drawback is that the range is limited.

    0 讨论(0)
  • 2020-11-22 02:51
    bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1)  return false;
        if (n <= 3)  return true;
    
        // This is checked so that we can skip 
        // middle five numbers in below loop
        if (n%2 == 0 || n%3 == 0) return false;
    
        for (int i=5; i*i<=n; i=i+6)
            if (n%i == 0 || n%(i+2) == 0)
               return false;
    
        return true;
    }
    

    this is just c++ implementation of above AKS algorithm

    0 讨论(0)
  • 2020-11-22 02:51

    I have got a prime function which works until (2^61)-1 Here:

    from math import sqrt
    def isprime(num): num > 1 and return all(num % x for x in range(2, int(sqrt(num)+1)))
    

    Explanation:

    The all() function can be redefined to this:

    def all(variables):
        for element in variables:
            if not element: return False
        return True
    

    The all() function just goes through a series of bools / numbers and returns False if it sees 0 or False.

    The sqrt() function is just doing the square root of a number.

    For example:

    >>> from math import sqrt
    >>> sqrt(9)
    >>> 3
    >>> sqrt(100)
    >>> 10
    

    The num % x part returns the remainder of num / x.

    Finally, range(2, int(sqrt(num))) means that it will create a list that starts at 2 and ends at int(sqrt(num)+1)

    For more information about range, have a look at this website!

    The num > 1 part is just checking if the variable num is larger than 1, becuase 1 and 0 are not considered prime numbers.

    I hope this helped :)

    0 讨论(0)
  • 2020-11-22 02:51
    myInp=int(input("Enter a number: "))
    if myInp==1:
        print("The number {} is neither a prime not composite no".format(myInp))
    elif myInp>1:
        for i in range(2,myInp//2+1):
            if myInp%i==0:
                print("The Number {} is not a prime no".format(myInp))
                print("Because",i,"times",myInp//i,"is",myInp)
                break
        else:
            print("The Number {} is a prime no".format(myInp))
    else:
        print("Alas the no {} is a not a prime no".format(myInp))
    
    0 讨论(0)
  • 2020-11-22 02:52

    In Python:

    def is_prime(n):
        return not any(n % p == 0 for p in range(2, int(math.sqrt(n)) + 1))
    

    A more direct conversion from mathematical formalism to Python would use all(n % p != 0... ), but that requires strict evaluation of all values of p. The not any version can terminate early if a True value is found.

    0 讨论(0)
提交回复
热议问题