Check whether X can be represented in the form of (2n – 1) / 3
Given an integer X, the task is to check whether X can be represented in the form of X = (2n – 1) / 3 for some value of n, Where it is necessary for X to be prime.
Examples:
Input: X = 5
Output: Yes
Explanation: X is a prime number and (24 – 1) / 3 = (16 – 1) / 3 = 15 / 3 = 5. So, it is valid.
Input: X = 2
Output: No
Approach: This can be solved with the following idea:
Simplifying, The equation given: X = (2n – 1) / 3
- 3X = 2n – 1
- 3X + 1 = 2n
- 2n = 3X + 1
Taking log2 Both the side of the equation, we get
- log2(2n) = log2(3X + 1)
- n = log2(3X + 1).
Therefore, if X is prime and after solving equation n is also prime, then it is valid
Steps involved in the implementation of code:
- Input a number X.
- Check whether the number is prime or not. If it is not prime then for sure It is not valid.
- Compute the logarithm of 3X+1 and store it in a variable n.
- Now we will check if n is a prime number or not.
- If n is a prime number then X is valid else it is not valid.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime( int n)
{
if (n == 2)
return false ;
int l = sqrt (n);
for ( int i = 3; i <= l; i += 2) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
bool isInteger( float num)
{
float n = floor (num);
if (num > n) {
return false ;
}
return true ;
}
bool isValid( int X)
{
if (isPrime(X) == false ) {
return false ;
}
float n = log2(3 * X + 1);
if (isInteger(n) == false ) {
return false ;
}
return isPrime(( int )n);
}
int main()
{
int X = 5;
if (isValid(X)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
|
Java
import java.io.*;
class GFG {
static boolean isPrime( int n)
{
if (n == 2 )
return false ;
int l = ( int )Math.sqrt(n);
for ( int i = 3 ; i <= l; i += 2 ) {
if (n % i == 0 ) {
return false ;
}
}
return true ;
}
static boolean isInteger( float num)
{
float n = ( float )Math.floor(num);
if (num > n) {
return false ;
}
return true ;
}
static boolean isValid( int X)
{
if (isPrime(X) == false ) {
return false ;
}
float n
= ( float )(Math.log( 3 * X + 1 ) / Math.log( 2 ));
if (isInteger(n) == false ) {
return false ;
}
return isPrime(( int )n);
}
public static void main(String[] args)
{
int X = 5 ;
if (isValid(X)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def isPrime(n):
if n = = 2 :
return False
l = int (math.sqrt(n))
for i in range ( 3 , l + 1 , 2 ):
if n % i = = 0 :
return False
return True
def isInteger(num):
n = math.floor(num)
if num > n:
return False
return True
def isValid(X):
if isPrime(X) = = False :
return False
n = math.log2( 3 * X + 1 )
if isInteger(n) = = False :
return False
return isPrime( int (n))
X = 5
if isValid(X):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static bool isPrime( int n)
{
if (n == 2)
return false ;
int l = ( int )Math.Sqrt(n);
for ( int i = 3; i <= l; i += 2)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static bool isInteger( float num)
{
float n = ( float )Math.Floor(num);
if (num > n)
{
return false ;
}
return true ;
}
static bool isValid( int X)
{
if (isPrime(X) == false )
{
return false ;
}
float n = ( float )Math.Log(3 * X + 1, 2);
if (isInteger(n) == false )
{
return false ;
}
return isPrime(( int )n);
}
static void Main()
{
int X = 5;
if (isValid(X))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function isPrime(n) {
if (n == 2) {
return false ;
}
let l = Math.sqrt(n);
for (let i = 3; i <= l; i += 2) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
function isInteger(num) {
let n = Math.floor(num);
if (num > n) {
return false ;
}
return true ;
}
function isValid(X) {
if (isPrime(X) == false ) {
return false ;
}
let n = Math.log(3 * X + 1) / Math.log(2);
if (isInteger(n) == false ) {
return false ;
}
return isPrime(parseInt(n));
}
let X = 5;
if (isValid(X)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Last Updated :
04 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...