Check whether X can be represented in the form of (2n - 1) / 3

Last Updated : 4 Apr, 2023

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++
// C++ code for the above approach
#include <iostream>
#include <math.h>
using namespace std;

// Function to check whether the
// number is prime or not
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;
}

// Function to check whether the number
// obtained is proper integer or not
bool isInteger(float num)
{

    // n will store integer value of num
    float n = floor(num);

    if (num > n) {
        return false;
    }
    return true;
}

// Function to check whether
// the number is valid or not
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);
}

// Driver code
int main()
{
    int X = 5;

    // Function call
    if (isValid(X)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
Java
// Java code for the above approach

import java.io.*;

class GFG {

  // Function to check whether the number is prime or not
  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;
  }

  // Function to check whether the number obtained is
  // proper integer or not
  static boolean isInteger(float num)
  {

    // n will store integer value of num
    float n = (float)Math.floor(num);

    if (num > n) {
      return false;
    }
    return true;
  }

  // Function to check whether the number is valid or not
  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;

    // Function call
    if (isValid(X)) {
      System.out.println("Yes");
    }
    else {
      System.out.println("No");
    }
  }
}

// This code is contributed by lokesh.
Python3
import math

# Function to check whether the
# number is prime or not
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

# Function to check whether the number
# obtained is proper integer or not
def isInteger(num):
    # n will store integer value of num
    n = math.floor(num)
    if num > n:
        return False
    return True

# Function to check whether
# the number is valid or not
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))

# Driver code
X = 5

# Function call
if isValid(X):
    print("Yes")
else:
    print("No")
C#
// C# code for the above approach

using System;

public class GFG {
    // Function to check whether the
    // number is prime or not
    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;
    }

    // Function to check whether the number
    // obtained is proper integer or not
    static bool isInteger(float num)
    {
        // n will store integer value of num
        float n = (float)Math.Floor(num);

        if (num > n)
        {
            return false;
        }
        return true;
    }

    // Function to check whether
    // the number is valid or not
    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);
    }

    // Driver code
    static void Main()
    {
        int X = 5;

        // Function call
        if (isValid(X))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// Function to check whether the number is prime or not
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 to check whether the number obtained is proper integer or not
function isInteger(num) {
  // n will store integer value of num
  let n = Math.floor(num);
  if (num > n) {
    return false;
  }
  return true;
}

// Function to check whether the number is valid or not
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));
}

// Driver Code

  let X = 5;

  // Function call
  if (isValid(X)) {
    console.log("Yes");
  } else {
    console.log("No");
  }

Output
Yes

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)

Comment