Open In App

Find that the value of 2^{2n} is equal to O(2^n) or not

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer n, the task is to find that the value of 2^{2n} is equal to O(2^n) or not.

Examples: 

Input: n = 2
Output: True

Input: n = 3
Output: False

Proof for the above equation:

We say that f(x) = O(g(x)) if and only if there exist positive constants c and k such that |f(x)| ≤ c|g(x)| for all x ≥ k. So, We need to find constants c and n0 such that |2^{2n}| ≤ c|2^n| for all n > n0.

  • Using the properties of exponents, we can rewrite 2^{2n} as (2^n)^2. So we have: ^{2n}| = |(2^n)^2| = (2^n)^2  
  • Similarly, we can write 2^n as 2*2^{n-1}, so we have: |2^n| = |2*2^{n-1}| = 2|2^{n-1}| 
  • Substituting these expressions into the inequality above, we get  (2^n)^2 ≤ c * 2|2^{n-1}| 
  • Simplifying the right-hand side, we get: (2^n)^2 ≤ 2c * (2^n)
  • Dividing both sides by (2^n), we get: 2^n ≤ 2c

So if we take c = 1, then we have 2^n ≤ 2, or equivalently, n ≥ 1. Therefore, we can conclude that 2^{2n} = O(2^n) with c = 1 and n0 = 1.

Steps involved in the implementation of the code:

  • We calculate the values of 2^{n} and 2^{2n} using the pow() function from the cmath library.
  • Finally, we check if 2^{2n} is upper-bounded by 2^{n} by comparing the two values.
  • If 2^{2n} is indeed upper-bounded by 2^{n}, we print the message “True“.
  • Otherwise, we print the message “False“.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
#include <cmath>
#include <iostream>
using namespace std;
 
// Function to check whether statements
// are equal or not
void check(int n)
{
 
    // Calculate 2^n and 2^(2n)
    int power_n = pow(2, n);
    int power_2n = pow(2, 2 * n);
 
    // Check if 2^(2n) is
    // upper-bounded by 2^n
    if (power_2n <= 4 * power_n)
        cout << "True" << endl;
    else
        cout << "False" << endl;
}
 
// Driver code
int main()
{
 
    int n = 2;
 
    // Function call
    check(n);
 
    return 0;
}


Java




// Java code for the above approach
import java.lang.Math;
 
public class GFG {
    public static void check(int n)
    {
        int power_n = (int)Math.pow(2, n);
        int power_2n = (int)Math.pow(2, 2 * n);
 
        if (power_2n <= 4 * power_n)
            System.out.println("True");
        else
            System.out.println("False");
    }
 
    public static void main(String[] args)
    {
        int n = 2;
        check(n);
    }
}
 
// This code is contributed by Susobhan Akhuli


Python3




import math
 
# Function to check whether statements
# are equal or not
 
 
def check(n):
 
    # Calculate 2^n and 2^(2n)
    power_n = 2 ** n
    power_2n = 2 ** (2 * n)
 
    # Check if 2^(2n) is
    # upper-bounded by 2^n
    if power_2n <= 4 * power_n:
        print("True")
    else:
        print("False")
 
 
# Driver code
if __name__ == "__main__":
 
    n = 2
 
    # Function call
    check(n)


C#




// C# code for the above approach
using System;
 
class Program {
  static void check(int n)
  {
    int power_n = (int)Math.Pow(2, n);
    int power_2n = (int)Math.Pow(2, 2 * n);
 
    if (power_2n <= 4 * power_n)
      Console.WriteLine("True");
    else
      Console.WriteLine("False");
  }
 
  static void Main(string[] args)
  {
    int n = 2;
    check(n);
  }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




// JavaScript code for the above approach
function check(n) {
 
    // Calculate 2^n and 2^(2n)
    let power_n = Math.pow(2, n);
    let power_2n = Math.pow(2, 2 * n);
 
    // Check if 2^(2n) is upper-bounded by 2^n
    if (power_2n <= 4 * power_n) {
        console.log("True");
    }
    else {
        console.log("False");
    }
}
 
// Driver code
let n = 2;
 
// Function call
check(n);
 
// This code is contributed by Susobhan Akhuli


Output

True

Time Complexity: O(logn), for pow function.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads