Determining if a number is prime

后端 未结 20 1289
悲哀的现实
悲哀的现实 2020-11-30 03:05

I have perused a lot of code on this topic, but most of them produce the numbers that are prime all the way up to the input number. However, I need code which only checks w

相关标签:
20条回答
  • 2020-11-30 03:17

    I came up with this:

    int counter = 0;
    
    bool checkPrime(int x) {
       for (int y = x; y > 0; y--){
          if (x%y == 0) {
             counter++;
          }
       }
       if (counter == 2) {
          counter = 0; //resets counter for next input
          return true; //if its only divisible by two numbers (itself and one) its a prime
       }
       else counter = 0;
            return false;
    }
    
    0 讨论(0)
  • 2020-11-30 03:19

    You need to do some more checking. Right now, you are only checking if the number is divisible by 2. Do the same for 2, 3, 4, 5, 6, ... up to number. Hint: use a loop.

    After you resolve this, try looking for optimizations. Hint: You only have to check all numbers up to the square root of the number

    0 讨论(0)
  • 2020-11-30 03:21

    Someone above had the following.

    bool check_prime(int num) {
    for (int i = num - 1; i > 1; i--) {
        if ((num % i) == 0)
            return false;
    }
    return true;
    }
    

    This mostly worked. I just tested it in Visual Studio 2017. It would say that anything less than 2 was also prime (so 1, 0, -1, etc.)

    Here is a slight modification to correct this.

    bool check_prime(int number)
    {
        if (number > 1)
        {
            for (int i = number - 1; i > 1; i--)
            {
                if ((number % i) == 0)
                    return false;
            }
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-30 03:21

    There are several different approches to this problem.
    The "Naive" Method: Try all (odd) numbers up to (the root of) the number.
    Improved "Naive" Method: Only try every 6n ± 1.
    Probabilistic tests: Miller-Rabin, Solovay-Strasse, etc.

    Which approach suits you depends and what you are doing with the prime.
    You should atleast read up on Primality Testing.

    0 讨论(0)
  • 2020-11-30 03:22

    C++

    bool isPrime(int number){
        if (number != 2){
            if (number < 2 || number % 2 == 0) {
                return false;
            }
            for(int i=3; (i*i)<=number; i+=2){
                if(number % i == 0 ){
                    return false;
                }
            }
        }
        return true;
    }
    

    Javascript

    function isPrime(number)
    {
        if (number !== 2) {
            if (number < 2 || number % 2 === 0) {
                return false;
            }
            for (var i=3; (i*i)<=number; i+=2)
            {
                if (number % 2 === 0){
                    return false;
                }
            }
        }
        return true;
    }
    

    Python

    def isPrime(number):
        if (number != 2):
            if (number < 2 or number % 2 == 0):
                return False
            i = 3
            while (i*i) <= number:
                if(number % i == 0 ):
                    return False;
                i += 2
        return True;
    
    0 讨论(0)
  • 2020-11-30 03:24

    Use mathematics first find square root of number then start loop till the number ends which you get after square rooting. check for each value whether the given number is divisible by the iterating value .if any value divides the given number then it is not a prime number otherwise prime. Here is the code

     bool is_Prime(int n)
     {
    
       int square_root = sqrt(n); // use math.h
       int toggle = 1;
       for(int i = 2; i <= square_root; i++)
       {
         if(n%i==0)
         { 
            toggle = 0;
            break;
         }
       }
    
       if(toggle)
         return true;
       else
         return false;
    
     } 
    
    0 讨论(0)
提交回复
热议问题