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

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

    Most of previous answers are correct but here is one more way to test to see a number is prime number. As refresher, prime numbers are whole number greater than 1 whose only factors are 1 and itself.(source)

    Solution:

    Typically you can build a loop and start testing your number to see if it's divisible by 1,2,3 ...up to the number you are testing ...etc but to reduce the time to check, you can divide your number by half of the value of your number because a number cannot be exactly divisible by anything above half of it's value. Example if you want to see 100 is a prime number you can loop through up to 50.

    Actual code:

    def find_prime(number):
        if(number ==1):
            return False
        # we are dividiing and rounding and then adding the remainder to increment !
        # to cover not fully divisible value to go up forexample 23 becomes 11
        stop=number//2+number%2
        #loop through up to the half of the values
        for item in range(2,stop):
            if number%item==0:
               return False
            print(number)
        return True
    
    
    if(find_prime(3)):
        print("it's a prime number !!")
    else:
        print("it's not a prime")  
    
    0 讨论(0)
  • 2020-11-22 02:33

    Way too late to the party, but hope this helps. This is relevant if you are looking for big primes:

    To test large odd numbers you need to use the Fermat-test and/or Miller-Rabin test.

    These tests use modular exponentiation which is quite expensive, for n bits exponentiation you need at least n big int multiplication and n big int divison. Which means the complexity of modular exponentiation is O(n³).

    So before using the big guns, you need to do quite a few trial divisions. But don't do it naively, there is a way to do them fast. First multiply as many primes together as many fits into a the words you use for the big integers. If you use 32 bit words, multiply 3*5*7*11*13*17*19*23*29=3234846615 and compute the greatest common divisor with the number you test using the Euclidean algorithm. After the first step the number is reduced below the word size and continue the algorithm without performing complete big integer divisions. If the GCD != 1, that means one of the primes you multiplied together divides the number, so you have a proof it's not prime. Then continue with 31*37*41*43*47 = 95041567, and so on.

    Once you tested several hundred (or thousand) primes this way, you can do 40 rounds of Miller-Rabin test to confirm the number is prime, after 40 rounds you can be certain the number is prime there is only 2^-80 chance it's not (it's more likely your hardware malfunctions...).

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

    Python 3:

    def is_prime(a):
        return a > 1 and all(a % i for i in range(2, int(a**0.5) + 1))
    
    0 讨论(0)
  • 2020-11-22 02:35

    In Python 3:

    def is_prime(a):
        if a < 2:
            return False
        elif a!=2 and a % 2 == 0:
            return False
        else:
            return all (a % i for i in range(3, int(a**0.5)+1))
    

    Explanation: A prime number is a number only divisible by itself and 1. Ex: 2,3,5,7...

    1) if a<2: if "a" is less than 2 it is not a prime.

    2) elif a!=2 and a % 2 == 0: if "a" is divisible by 2 then its definitely not a prime. But if a=2 we don't want to evaluate that as it is a prime number. Hence the condition a!=2

    3) return all (a % i for i in range(3, int(a0.5)+1) ):** First look at what all() command does in python. Starting from 3 we divide "a" till its square root (a**0.5). If "a" is divisible the output will be False. Why square root? Let's say a=16. The square root of 16 = 4. We don't need to evaluate till 15. We only need to check till 4 to say that it's not a prime.

    Extra: A loop for finding all the prime number within a range.

    for i in range(1,100):
        if is_prime(i):
            print("{} is a prime number".format(i))
    
    0 讨论(0)
  • 2020-11-22 02:35
    import math
    import time
    
    
    def check_prime(n):
    
        if n == 1:
            return False
    
        if n == 2:
            return True
    
        if n % 2 == 0:
            return False
    
        from_i = 3
        to_i = math.sqrt(n) + 1
    
        for i in range(from_i, int(to_i), 2):
            if n % i == 0:
                return False
        return True
    
    0 讨论(0)
  • 2020-11-22 02:37

    A prime number is any number that is only divisible by 1 and itself. All other numbers are called composite.

    The simplest way, of finding a prime number, is to check if the input number is a composite number:

        function isPrime(number) {
            // Check if a number is composite
            for (let i = 2; i < number; i++) {
                if (number % i === 0) {
                    return false;
                }
            }
            // Return true for prime numbers
            return true;
        }
    

    The program has to divide the value of number by all the whole numbers from 1 and up to the its value. If this number can be divided evenly not only by one and itself it is a composite number.

    The initial value of the variable i has to be 2 because both prime and composite numbers can be evenly divided by 1.

        for (let i = 2; i < number; i++)
    

    Then i is less than number for the same reason. Both prime and composite numbers can be evenly divided by themselves. Therefore there is no reason to check it.

    Then we check whether the variable can be divided evenly by using the remainder operator.

        if (number % i === 0) {
            return false;
        }
    

    If the remainder is zero it means that number can be divided evenly, hence being a composite number and returning false.

    If the entered number didn't meet the condition, it means it's a prime number and the function returns true.

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