Determining if a number is prime

后端 未结 20 1288
悲哀的现实
悲哀的现实 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:26
    #define TRUE 1
    #define FALSE -1
    
    int main()
    {
    /* Local variables declaration */
    int num = 0;
    int result = 0;
    
    /* Getting number from user for which max prime quadruplet value is 
    to be found */
    printf("\nEnter the number :");
    scanf("%d", &num);
    
    result = Is_Prime( num );
    
    /* Printing the result to standard output */
    if (TRUE == result)
        printf("\n%d is a prime number\n", num);
    else
        printf("\n%d is not a prime number\n", num);
    
    return 0;
    }
    
    int Is_Prime( int num )
    {
    int i = 0;
    
    /* Checking whether number is negative. If num is negative, making
    it positive */
    if( 0 > num )
        num = -num;
    
    /* Checking whether number is less than 2 */
    if( 2 > num )
        return FALSE;
    
    /* Checking if number is 2 */
    if( 2 == num )
        return TRUE;
    
    /* Checking whether number is even. Even numbers
    are not prime numbers */
    if( 0 == ( num % 2 ))
        return FALSE;
    
    /* Checking whether the number is divisible by a smaller number
    1 += 2, is done to skip checking divisibility by even numbers.
    Iteration reduced to half */
    for( i = 3; i < num; i += 2 )
        if( 0 == ( num % i ))
            /* Number is divisible by some smaller number, 
            hence not a prime number */
            return FALSE;
    
    return TRUE;
    }
    
    0 讨论(0)
  • 2020-11-30 03:27
    bool isPrime(int number){
    
        if(number < 2) return false;
        if(number == 2) return true;
        if(number % 2 == 0) return false;
        for(int i=3; (i*i)<=number; i+=2){
            if(number % i == 0 ) return false;
        }
        return true;
    
    }
    
    0 讨论(0)
  • 2020-11-30 03:27

    I Have Use This Idea For Finding If The No. Is Prime or Not:

    #include <conio.h> 
    #include <iostream>
    using namespace std;
    int main() {
      int x, a;
      cout << "Enter The No. :";
      cin >> x;
      int prime(unsigned int);
      a = prime(x);
      if (a == 1)
        cout << "It Is A Prime No." << endl;
      else
      if (a == 0)
        cout << "It Is Composite No." << endl;
      getch();
    }
    
    int prime(unsigned int x) {
      if (x == 1) {
        cout << "It Is Neither Prime Nor Composite";
        return 2;
      }
      if (x == 2 || x == 3 || x == 5 || x == 7)
        return 1;
      if (x % 2 != 0 && x % 3 != 0 && x % 5 != 0 && x % 7 != 0)
        return 1;
      else
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 03:29

    If n is 2, it's prime.

    If n is 1, it's not prime.

    If n is even, it's not prime.

    If n is odd, bigger than 2, we must check all odd numbers 3..sqrt(n)+1, if any of this numbers can divide n, n is not prime, else, n is prime.

    For better performance i recommend sieve of eratosthenes.

    Here is the code sample:

    bool is_prime(int n)
    {
      if (n == 2) return true;
      if (n == 1 || n % 2 == 0) return false;
    
      for (int i = 3; i*i < n+1; i += 2) {
          if (n % i == 0) return false;
      }
    
      return true;
    }
    
    0 讨论(0)
  • 2020-11-30 03:33

    If you are lazy, and have a lot of RAM, create a sieve of Eratosthenes which is practically a giant array from which you kicked all numbers that are not prime. From then on every prime "probability" test will be super quick. The upper limit for this solution for fast results is the amount of you RAM. The upper limit for this solution for superslow results is your hard disk's capacity.

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

    I follow same algorithm but different implementation that loop to sqrt(n) with step 2 only odd numbers because I check that if it is divisible by 2 or 2*k it is false. Here is my code

    public class PrimeTest {
    
        public static boolean isPrime(int i) {
            if (i < 2) {
                return false;
            } else if (i % 2 == 0 && i != 2) {
                return false;
            } else {
                for (int j = 3; j <= Math.sqrt(i); j = j + 2) {
                    if (i % j == 0) {
                        return false;
                    }
                }
                return true;
            }
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 1; i < 100; i++) {
                if (isPrime(i)) {
                    System.out.println(i);
                }
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题