Prime Numbers in Discrete Mathematics - GeeksforGeeks

27 Aug, 2021
Overview :An integer p>1 is called a prime number, or prime if the only positive divisors of p are 1 and p. An integer q>1 that is not prime is called composite.
Example –The integers 2,3,5,7 and 11 are prime numbers, and the integers 4,6,8, and 9 are composite.
Theorem-1: An integer p>1 is prime if and only if for all integers a and b, p divides ab implies either p divides a or p divides b.
Example – Consider the integer 12.Now 12 divides 120 = 30 x 4 but 12|30 and 12|4.Hence,12 is not prime.
Theorem-2 : Every integer n>=2 has a prime factor.
Theorem-3 : If n is a composite integer, then n has a prime factor not exceeding √n.
Example-1 :Determine which of the following integers are prime?
a) 293 b) 9823
Solution –
We first find all primes p such that p2< = 293.These primes are 2,3,5,7,11,13 and 17.Now, none of these primes divide 293. Hence, 293 is a prime.We consider primes p such that p2< = 9823.These primes are 2,3,5,7,11,13,17, etc. None of 2,3,5,7 can divide 9823. However,11 divides 9823.Hence, 9823 is not a prime.
We first find all primes p such that p2< = 293.These primes are 2,3,5,7,11,13 and 17.Now, none of these primes divide 293. Hence, 293 is a prime.
We consider primes p such that p2< = 9823.These primes are 2,3,5,7,11,13,17, etc. None of 2,3,5,7 can divide 9823. However,11 divides 9823.Hence, 9823 is not a prime.
Example-2 : Let n be a positive integer such that n2-1 is prime. Then n =?
Solution – We can write, n2-1 = (n-1)(n2+n+1). Because n3-1 is prime, either n-1 = 1 or n2+n+1 = 1.Now n>=1, So n2+n+1 > 1,i.e., n2+n+1 != 1.Thus, we must have n-1 = 1.This implies that n=2.
Example-3 :Let p be a prime integer such that gcd(a, p3)=p and gcd(b,p4)=p. Find gcd(ab,p7).
Solution – By the given condition, gcd(a,p3)=p. Therefore, p | a. Also, p2|a.(For if p2| a, then gcd (a,p3)>=p2>p, which is a contradiction.) Now a can be written as a product of prime powers. Because p|a and p2| a, it follows that p appears as a factor in the prime factorization of a, but pk, where k>=2, does not appear in that prime factorization. Similarly ,gcd(b,p4)=p implies that p|b and p2|b. As before, it follows that p appears as a factor in the prime factorization of a, but pk, where k>=2, does not appear in that prime factorization. It now follows that p2|ab and p3|ab. Hence, gcd(b,p7) = p2 .
Primality Test Algorithm :
for i: [2,N-1]
  if i divides N
     return "Composite"
     
return "Prime"        
Example –Let’s take an example and makes the algorithm more efficient, 36=
Take the inputs of a and b until,    
 a<=b
                                                                
 a . b = N
 a . N/a = N
Modified algorithm :
for i : [2,√n]
   if i divides N
     return "Composite"
     
return "Prime"    
C++
Java
Python3
Javascript
// C++ program to check primality of a number#include <bits/stdc++.h>using namespace std; bool is_prime(int n){   for(int i = 2; i * i <= n; i++){     if(n % i == 0){       return false;       //if the number is composite or not prime.     }   }  return true;    // number is prime.}int main() {     int n;    cin >> n;    cout << is_prime(n) ? "prime" : "composite";    return 0;}
// Java program to check primality of a number /*package whatever //do not write package name here */ import java.util.*;class prime{    public Boolean is_prime(int n){        for(int i = 2; i * i <= n; i++){            if(n % i == 0){                return false;              //if the number is composite or not prime.            }        }        return true;      // number is prime.    } } class GFG {    public static void main (String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        prime ob = new prime();        System.out.println(ob.is_prime(n) ? "Prime" : "Composite");    }}
# Python program to check primality of a numberimport math  def is_prime(n):    for i in range(2, int(math.sqrt(n))):        if(n % i == 0):            return False        # if the number is composite or not prime.     return True    #  number is prime.  if __name__ == "__main__":     n = int(input())    if is_prime(n):        print("prime")    else:        print("composite")     # This code is contributed by rakeshsahni
<script>// JavaScript program to check primality of a numberfunction is_prime(n){   for(var i = 2; i * i <= n; i++)   {     if(n % i == 0){       return false;               // if the number is composite or not prime.     }   }  return true;    // number is prime.}    var n;    document.write(is_prime(n) ? "prime" : "composite");     // This code is contributed by shivanisinghss2110</script>
Algorithm to find prime numbers :In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
Algorithm Sieve of Eratosthenes is input: an integer n > 1.
output : all prime numbers from 2 through n.

let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
    
    for i = 2, 3, 4, ..., not exceeding √n do
        if A[i] is true
            for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
                A[j] := false

    return all i such that A[i] is true.
rakeshsahni
shivanisinghss2110
Misc
Prime Number
Engineering Mathematics
GATE CS
Misc
Misc
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Activation Functions
Difference between Propositional Logic and Predicate Logic
Logic Notations in LaTeX
Univariate, Bivariate and Multivariate data and its analysis
Z-test
Layers of OSI Model
ACID Properties in DBMS
TCP/IP Model
Types of Operating Systems
Normal Forms in DBMS