Mersenne Prime Number

Here given code implementation process.

/*
  C Program 
  Check if a given number is Mersenne Prime or not
*/
#include<stdio.h>

int is_prime(int n)
{
  if(n<=1)
  {
    return 0;
  }
  //Base case
  if(n==2 || n ==3 || n==5)
  {
    return 1;
  }
  //Check divisibility of a number
  for (int i = n/2; i > 1 ; --i)
  {
    if(n%i == 0)
    {
      return 0;
    }
  }
  
  return 1;
}

int is_mersenne_prime(int number)
{
  int remainder = 0;

  while (number != 0) 
  { 
    remainder = number % 2; 
    //Check the divisibility remainder
    if (remainder == 0) 
    {
      return 0; 
    }
    //Remove last digit
    number /= 2; 
  } 
  return 1;
}

//Determine whether number is Mersenne prime
void check_mersenne_no(int number)
{
  //Test case of mersenne no
  if(number > 1 && is_prime(number) && is_mersenne_prime(number) == 1)
  {
    printf("%d is a mersenne number\n",number );
   
  }
  else
  {
    printf("%d is not a mersenne number\n",number );
  }
}
int main(){

  //Test
  check_mersenne_no(17);
  check_mersenne_no(31);
  check_mersenne_no(42);
  check_mersenne_no(13);
  check_mersenne_no(7);
  return 0;
}

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
/*
 C++ Program
 Check if a given number is Mersenne Prime or not
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:
		bool is_prime(int n) {
			if (n <= 1) {
				return false;
			}
			//Base case

			if (n == 2 || n == 3 || n == 5) {
				return true;
			}
			//Check divisibility of a number

			for (int i = n / 2; i > 1; --i) {
				if (n % i == 0) {
					return false;
				}
			}
			return true;
		}
	bool is_mersenne_prime(int number) {
		int remainder = 0;
		while (number != 0) {
			remainder = number % 2;
			//Check the divisibility remainder

			if (remainder == 0) {
				return false;
			}
			//Remove last digit
			number /= 2;
		}
		return true;
	}
	//Determine whether number is Mersenne prime
	void check_mersenne_no(int number) {
		//Test case of mersenne prime number

		if (number > 1 && this->is_prime(number) == true && this->is_mersenne_prime(number) == true) {
			cout << number << " is a mersenne number\n";
		} else {
			cout << number << " is not a mersenne number\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.check_mersenne_no(17);
	obj.check_mersenne_no(31);
	obj.check_mersenne_no(42);
	obj.check_mersenne_no(13);
	obj.check_mersenne_no(7);
	return 0;
}

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
/*
  Java Program
  Check if a given number is Mersenne Prime or not
*/
public class MyNumber {
  public boolean is_prime(int n)
  {
    if(n<=1)
    {
      return false;
    }
    //Base case
    if(n==2 || n ==3 || n==5)
    {
      return true;
    }
    //Check divisibility of a number
    for (int i = n/2; i > 1 ; --i)
    {
      if(n%i == 0)
      {
        return false;
      }
    }
    
    return true;
  }

  public boolean is_mersenne_prime(int number)
  {
    int remainder = 0;

    while (number != 0) 
    { 
      remainder = number % 2; 
      //Check the divisibility remainder
      if (remainder == 0) 
      {
        return false; 
      }
      //Remove last digit
      number /= 2; 
    } 
    return true;
  }

  //Determine whether number is Mersenne prime
  public void check_mersenne_no(int number)
  {
    //Test case of mersenne prime number
    if(number > 1 && is_prime(number) == true && is_mersenne_prime(number) == true)
    {
      System.out.print(number+" is a mersenne number\n" );
    }
    else
    {
      System.out.print(number+" is not a mersenne number\n" );
    }
  }
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Case
    obj.check_mersenne_no(17);
    obj.check_mersenne_no(31);
    obj.check_mersenne_no(42);
    obj.check_mersenne_no(13);
    obj.check_mersenne_no(7);
  }
}

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
/*
  C# Program
  Check if a given number is Mersenne Prime or not
*/
using System;
public class MyNumber {
	public Boolean is_prime(int n) {
		if (n <= 1) {
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		//Check divisibility of a number
		for (int i = n / 2; i > 1; --i) {
			if (n % i == 0) {
				return false;
			}
		}

		return true;
	}

	public Boolean is_mersenne_prime(int number) {
		int reMainder = 0;

		while (number != 0) {
			reMainder = number % 2;
			//Check the divisibility reMainder
			if (reMainder == 0) {
				return false;
			}
			//Remove last digit
			number /= 2;
		}
		return true;
	}

	//Determine whether number is Mersenne prime
	public void check_mersenne_no(int number) {
		//Test case of mersenne prime number
		if (number > 1 && is_prime(number) == true && is_mersenne_prime(number) == true) {
			Console.Write(number + " is a mersenne number\n");
		} else {
			Console.Write(number + " is not a mersenne number\n");
		}
	}
	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Case
		obj.check_mersenne_no(17);
		obj.check_mersenne_no(31);
		obj.check_mersenne_no(42);
		obj.check_mersenne_no(13);
		obj.check_mersenne_no(7);
	}
}

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
<?php
/*
  Php Program
  Check if a given number is Mersenne Prime or not
*/
class MyNumber {
	public 	function is_prime($n) {
		if ($n <= 1) {
			return false;
		}
		//Base case

		if ($n == 2 || $n == 3 || $n == 5) {
			return true;
		}
		//Check divisibility of a number

		for ($i = intval($n / 2); $i > 1; --$i) {
			if ($n % $i == 0) {
				return false;
			}
		}
		return true;
	}
	public 	function is_mersenne_prime($number) {
		$remainder = 0;
		while ($number != 0) {
			$remainder = $number % 2;
			//Check the divisibility remainder

			if ($remainder == 0) {
				return false;
			}
          	//Remove last digit
			$number = intval($number / 2);
		}
		return true;
	}
	//Determine whether number is Mersenne prime

	public 	function check_mersenne_no($number) {
		//Test case of mersenne prime number

		if ($number > 1 && $this->is_prime($number) == true && $this->is_mersenne_prime($number) == true) {
			echo($number ." is a mersenne number\n");
		} else {
			echo($number ." is not a mersenne number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->check_mersenne_no(17);
	$obj->check_mersenne_no(31);
	$obj->check_mersenne_no(42);
	$obj->check_mersenne_no(13);
	$obj->check_mersenne_no(7);
}
main();

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
/*
 Node Js Program
 Check if a given number is Mersenne Prime or not
*/
class MyNumber {
	is_prime(n) {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		//Check divisibility of a number

		for (var i = parseInt(n / 2); i > 1; --i) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
	is_mersenne_prime(number) {
		var remainder = 0;
		while (number != 0) {
			remainder = number % 2;
			//Check the divisibility remainder

			if (remainder == 0) {
				return false;
			}
          	//Remove last digit
			number = parseInt(number / 2);
		}
		return true;
	}
	//Determine whether number is Mersenne prime
	check_mersenne_no(number) {
		//Test case of mersenne prime number

		if (number > 1 && this.is_prime(number) == true && this.is_mersenne_prime(number) == true) {
			process.stdout.write(number + " is a mersenne number\n");
		} else {
			process.stdout.write(number + " is not a mersenne number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.check_mersenne_no(17);
	obj.check_mersenne_no(31);
	obj.check_mersenne_no(42);
	obj.check_mersenne_no(13);
	obj.check_mersenne_no(7);
}
main();

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
# Python 3 Program
# Check if a given number is Mersenne Prime or not
class MyNumber :
	def is_prime(self, n) :
		if (n <= 1) :
			return False
		
		# Base case
		if (n == 2 or n == 3 or n == 5) :
			return True
		
		# Check divisibility of a number
		i = int(n / 2)
		while (i > 1) :
			if (n % i == 0) :
				return False
			
			i -= 1
		
		return True
	
	def is_mersenne_prime(self, number) :
		remainder = 0
		while (number != 0) :
			remainder = number % 2
			# Check the divisibility remainder
			if (remainder == 0) :
				return False
			# Remove last digit
			number = int(number / 2)
		
		return True
	
	# Determine whether number is Mersenne prime
	def check_mersenne_no(self, number) :
		# Test case of mersenne prime number

		if (number > 1 and self.is_prime(number) == True and self.is_mersenne_prime(number) == True) :
			print(number ," is a mersenne number")
		else :
			print(number ," is not a mersenne number")
		
	

def main() :
	obj = MyNumber()
	obj.check_mersenne_no(17)
	obj.check_mersenne_no(31)
	obj.check_mersenne_no(42)
	obj.check_mersenne_no(13)
	obj.check_mersenne_no(7)


if __name__ == "__main__":
	main()

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
# Ruby Program 
# Check if a given number is Mersenne Prime or not
class MyNumber 
	def is_prime(n) 
		if (n <= 1) 
			return false
		end
		# Base case

		if (n == 2 or n == 3 or n == 5) 
			return true
		end
		# Check divisibility of a number
		i = n / 2
		while (i > 1) 
			if (n % i == 0) 
				return false
			end
			i -= 1
		end
		return true
	end
	def is_mersenne_prime(number) 
		remainder = 0
		while (number != 0) 
			remainder = number % 2
			# Check the divisibility remainder
			if (remainder == 0) 
				return false
			end
			# Remove last digit
			number /= 2
		end
		return true
	end
	# Determine whether number is Mersenne prime
	def check_mersenne_no(number) 
		# Test case of mersenne prime number

		if (number > 1 and self.is_prime(number) == true and self.is_mersenne_prime(number) == true) 
			print(number ," is a mersenne number\n")
		else 
			print(number ," is not a mersenne number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.check_mersenne_no(17)
	obj.check_mersenne_no(31)
	obj.check_mersenne_no(42)
	obj.check_mersenne_no(13)
	obj.check_mersenne_no(7)
end
main()

Output

17 is not a mersenne number
31 is a mersenne number
42 is not a mersenne number
13 is not a mersenne number
7 is a mersenne number
/*
 Scala Program
 Check if a given number is Mersenne Prime or not
*/
class MyNumber {
	def is_prime(number: Int): Boolean = {
		var n: Int = number;
      	if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		
		//Check divisibility of a number
		var i: Int = (n / 2).toInt;
		
		while (i > 1) {
			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	def is_mersenne_prime(n: Int): Boolean = {
		var remainder: Int = 0;
      	var number: Int = n;
		while (number != 0) {
			remainder = number % 2;
			//Check the divisibility remainder

			if (remainder == 0) {
				return false;
			}
          	//Remove last digit
			number = (number / 2).toInt;
		}
		return true;
	}
	//Determine whether number is Mersenne prime
	def check_mersenne_no(number: Int): Unit = {
		//Test case of mersenne prime number

		if (number > 1 && this.is_prime(number) == true && this.is_mersenne_prime(number) == true) {
			print(s" $number is a mersenne number\n");
		} else {
			print(s" $number is not a mersenne number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.check_mersenne_no(17);
        obj.check_mersenne_no(31);
        obj.check_mersenne_no(42);
        obj.check_mersenne_no(13);
  obj.check_mersenne_no(7);
	}
}

Output

 17 is not a mersenne number
 31 is a mersenne number
 42 is not a mersenne number
 13 is not a mersenne number
 7 is a mersenne number
/*
  Swift 4 Program
  Check if a given number is Mersenne Prime or not
*/
class MyNumber {
	func is_prime(_ n: Int) -> Bool {
      
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		//Check divisibility of a number
		var i: Int = n / 2;
		while (i > 1) {
			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	func is_mersenne_prime(_ n: Int) -> Bool {
		var remainder: Int = 0;
      	var number: Int = n;
		while (number != 0) {
			remainder = number % 2;
			//Check the divisibility remainder
			if (remainder == 0) {
				return false;
			}
			//Remove last digit
			number /= 2;
		}
		return true;
	}
	//Determine whether number is Mersenne prime
	func check_mersenne_no(_ number: Int) {
		//Test case of mersenne prime number

		if (number > 1 && self.is_prime(number) == true && self.is_mersenne_prime(number) == true) {
			print(number ," is a mersenne number");
		} else {
			print(number ," is not a mersenne number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.check_mersenne_no(17);
	obj.check_mersenne_no(31);
	obj.check_mersenne_no(42);
	obj.check_mersenne_no(13);
	obj.check_mersenne_no(7);
}
main();

Output

17  is not a mersenne number
31  is a mersenne number
42  is not a mersenne number
13  is not a mersenne number
7  is a mersenne number

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved