Prime Numbers

1.8

A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 9 11 13 17 19 23 …..

Some interesting fact about Prime numbers

  1. 2 is the only even Prime number.
  2. every prime number can represented in form of 6n+1 or 6n-1, where n is natural number.
  3. 2, 3 are only two consecutive natural numbers which are prime too.
  4. Goldbach Conjecture: Every even integer greater than 2 can be expressed as the sum of two primes.
  5. GCD of a natural number with Prime is always one.
  6. Wilson Theorem : Wilson’s theorem states that a natural number p > 1 is a prime number if and only if
        (p - 1) ! ≡  -1   mod p 
    OR  (p - 1) ! ≡  (p-1) mod p
  7. Fermat’s Little Theorem: If n is a prime number, then for every a, 1 <= a < n,
    an-1 ≡ 1 (mod n)
     OR 
    an-1 % n = 1 
  8. Prime Number Theorem : The probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or to the logarithm of n.

How we check whether a number is Prime or not?

  1. Naive solution.
    A naive solution is to iterate through all numbers from 2 to n-1 and for every number check if it divides n. If we find any number that divides, we return false.

    C++

    // A school method based C++ program to 
    // check if a number is prime
    #include <bits/stdc++.h>
    using namespace std;
    
    // function check whether a number 
    // is prime or not
    bool isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;
    
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
    
        return true;
    }
    
    // Driver Program to test above function
    int main()
    {
        isPrime(11) ? cout << " true\n" : 
                      cout << " false\n";
        return 0;
    }
    

    Python3

    # A school method based Python3 program 
    # to check if a number is prime
    
    # function check whether a number 
    # is prime or not
    def isPrime(n):
        
        # Corner case
        if (n <= 1):
            return False
    
        # Check from 2 to n-1
        for i in range(2, n):
            if (n % i == 0):
                return False
    
        return True
    
    # Driver Program to test above function
    if isPrime(11):
        print ("true")
    else:
        print ("false")
        
    # This code is contributed by Sachin Bisht
    


    Output:
    Time complexity :O(n)

    True
    
  2. Efficient solutions

Algorithms to find all prime number smaller the N.

More problems related to Prime number


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.8 Average Difficulty : 1.8/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.