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

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

提交回复
热议问题