Prime Numbers


Prime number are the number which have only 2 divisor...1 and itself...
Example:
2 3 5 7 11 13 17 19 23 …..
Prime number have many application in computer science....ans also 
There are many methods to check weather a number is PRIME or not.
Example:
    • Fermat Method
    • Miller–Rabin
    • Solovay-Strassen
    • Lucas Primality Test


Different Algorithms to check the prime numbers:
  • Sieve of Eratosthenes
  • Segmented Sieve
  • Sieve of Sundaram
  • Bitwise Sieve
We will see each method and algorithm in different post.

In this Post we will discuss the basic method to test the number weather the number is Prime or not

Simple Approach:
        In this approach we will iterate over all the number from 1 to N-1 and check weather 
        that number divides the number or not....
        Here is simple CPP program for the above approach..

#include <bits/stdc++.h>
using namespace std;
  

bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
int main()
{
    int N=13;
bool flag=true;
if (N <= 1)
        flag= false;
else
{
    for (int i = 2; i < n; i++)
        if (n % i == 0) {
            flag false;
break;}
}

if(flag)
cout<<"YES";
else
cout<<"NO";
    return 0;
}



Time complexity :O(n)

This is the basic method to check the prime number ,in the next post we will see the seive algorithm to check the number in the range from L to


Here is an intresting application of prime number
https://letuscp.blogspot.com/2019/10/sphenic-number-sphenic-number-positive.html
  


Comments

Post a Comment

Popular posts from this blog

Fibonacci numbers with help of DP