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

后端 未结 30 2699
遇见更好的自我
遇见更好的自我 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:54

    Here's my take on the answer:

    def isprime(num):
        return num <= 3 or (num + 1) % 6 == 0 or (num - 1) % 6 == 0
    

    The function will return True if any of the properties below are True. Those properties mathematically define what a prime is.

    1. The number is less than or equal to 3
    2. The number + 1 is divisible by 6
    3. The number - 1 is divisible by 6
    0 讨论(0)
  • 2020-11-22 02:55

    Let me suggest you the perfect solution for 64 bit integers. Sorry to use C#. You have not already specified it as python in your first post. I hope you can find a simple modPow function and analyze it easily.

    public static bool IsPrime(ulong number)
    {
        return number == 2 
            ? true 
            : (BigInterger.ModPow(2, number, number) == 2 
                ? (number & 1 != 0 && BinarySearchInA001567(number) == false) 
                : false)
    }
    
    public static bool BinarySearchInA001567(ulong number)
    {
        // Is number in list?
        // todo: Binary Search in A001567 (https://oeis.org/A001567) below 2 ^ 64
        // Only 2.35 Gigabytes as a text file http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html
    }
    
    0 讨论(0)
  • 2020-11-22 02:56

    We can use java streams to implement this in O(sqrt(n)); Consider that noneMatch is a shortCircuiting method that stops the operation when finds it unnecessary for determining the result:

    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    System.out.println(n == 2 ? "Prime" : IntStream.rangeClosed(2, ((int)(Math.sqrt(n)) + 1)).noneMatch(a -> n % a == 0) ? "Prime" : "Not Prime");
    
    0 讨论(0)
  • 2020-11-22 02:57
    public static boolean isPrime(int number) {
     if(number < 2)
       return false;
     else if(number == 2 || number == 3)
            return true;
          else {
            for(int i=2;i<=number/2;i++)
               if(number%i == 0)
                 return false;
               else if(i==number/2)
                    return true;
          }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 02:58

    The best method, in my opinion, is to use what's gone before.

    There are lists of the first N primes on the internet with N stretching up to at least fifty million. Download the files and use them, it's likely to be much faster than any other method you'll come up with.

    If you want an actual algorithm for making your own primes, Wikipedia has all sorts of good stuff on primes here, including links to the various methods for doing it, and prime testing here, both probability-based and fast-deterministic methods.

    There should be a concerted effort to find the first billion (or even more) primes and get them published on the net somewhere so people can stop doing this same job over and over and over and ... :-)

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

    When I have to do a fast verification, I write this simple code based on the basic division between numbers lower than square root of input.

    def isprime(n):
        if n%2==0:
            return n==2
        else:
            cota = int(n**0.5)+1
            for ind in range(3,2,cota):
                if n%ind==0:
                    print(ind)
                    return False
        is_one = n==1
        return True != is_one
    
    isprime(22783)
    
    • The last True != n==1 is to avoid the case n=1.
    0 讨论(0)
提交回复
热议问题