Techno Blender
Digitally Yours.

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

0 37


Improve Article

Save Article

Like Article

Improve Article

Save Article

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++

#include <cmath>

#include <iostream>

using namespace std;

  

void check(int n)

{

  

    

    int power_n = pow(2, n);

    int power_2n = pow(2, 2 * n);

  

    

    

    if (power_2n <= 4 * power_n)

        cout << "True" << endl;

    else

        cout << "False" << endl;

}

  

int main()

{

  

    int n = 2;

  

    

    check(n);

  

    return 0;

}

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


Improve Article

Save Article

Like Article

Improve Article

Save Article

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++

#include <cmath>

#include <iostream>

using namespace std;

  

void check(int n)

{

  

    

    int power_n = pow(2, n);

    int power_2n = pow(2, 2 * n);

  

    

    

    if (power_2n <= 4 * power_n)

        cout << "True" << endl;

    else

        cout << "False" << endl;

}

  

int main()

{

  

    int n = 2;

  

    

    check(n);

  

    return 0;

}

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

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment