CODING JOY

The stunning potpourri of coding and mundane life

MERSENNE PRIME IN JAVA LANGUAGE



AMRUHA AHMED
10th November,2023.



Mersenne Primes are those numbers that are prime as well as they can be expressed using the formula:

2^n - 1

Where n is also a prime number.

blog1

FUNCTIONS REQUIRED:

VARIABLES REQUIRED IN PRIME FUNCTION:

VARIABLES REQUIRED IN MAIN FUNCTION:

ALGORITHM FOR MAIN FUNCTION:

PROGRAM:

                
import java.util.*;
class mersenneprime
{
    Scanner ob=new Scanner(System.in);
    boolean prime(int n)//checking if the number is prime or not
    {
        int i;//loop counter
        int ctr=0;//count of divisors
        for(i=1;i<=n;i++)
        {
            if(n%i==0)
            ctr++;
        }
        if(ctr==2)
        return true;//if number is prime
        else
        return false;//if number is not prime
    }
    void main()
    {
        int n;//number to be checked for mersenne prime
        int flag=0;//flag to check if the number is mersenne prime or not
        System.out.print("Enter a number:");
        n=ob.nextInt();        
        boolean result1=prime(n);//stores the result whether 'n' is prime or not
        for(int i=2;i<n;i++)//i is the loop counter
        {
             boolean result2=prime(i);//stores the result whether 'i' is prime or not
             if(result2==true && result1==true)//if both 'i' and 'n' are prime
             {
             int power2n=((int)Math.pow(2,i))-1;//calculating 2^i-1
             if(power2n==n)// if 2^i -1 is equal to n
             {
                 flag=1;
                 break;
                }
                 
            }
        }
        if(flag==1)
        System.out.println(n+" is a Mersenne Prime");
        else
        System.out.println(n+" is not a Mersenne Prime");
    }
}

             
         

DRY RUN:

If n=7 is given as input,then result1=true since 7 is a prime number and the values of i, flag , power2n and result2 will be as follows: blog1

OUTPUT:

Enter a number:7

7 is a Mersenne Prime