# Time complexity of ThreeToTheN

• 06-25-2013, 02:54 PM
Malv
Time complexity of ThreeToTheN
Write a class, called ThreeToTheN with a method with the signature:

public void run(int n);

which takes running time in proportion to 3n. Note, your run method does not need to produce any output or do anything else. It just needs to take the appropriate number of operations to run.

If it helps with your solution, you are allowed to define additional methods in the class.
• 06-25-2013, 02:55 PM
PhHein
Re: Time complexity of ThreeToTheN
What have you tried? - Matt Gemmell

• 06-25-2013, 02:58 PM
Malv
Re: Time complexity of ThreeToTheN
from what i know,

somehow got this code for 2^N

Code:

```public class FibMemMatch {     public void run(int n){         int a=0, b=1;                 for(int i=0; i<n; i++) {                     int c = a;                     a = b;                     b = c + b;                 }                 System.out.println(a);     } }```
How do i edit it so its time complexity is 3^N?
• 06-25-2013, 03:02 PM
Malv
Re: Time complexity of ThreeToTheN
I've try out codes like this

Code:

```public class ThreeToTheN {     public void run(int n){         int t= (int)Math.pow(3, n);         for (int o= 1; o == 1; o++) // outer loop             while (t-- > 0); // nested loop     } }```
but my teacher says no math.class
• 06-25-2013, 03:09 PM
PhHein
Re: Time complexity of ThreeToTheN
Use BigInteger then.
• 06-25-2013, 03:13 PM
Malv
Re: Time complexity of ThreeToTheN
BigInteger belongs to Math.class

java.Math.BigInteger
• 06-25-2013, 03:16 PM
PhHein
Re: Time complexity of ThreeToTheN
Then implement your own pow method.
• 06-25-2013, 04:12 PM
JosAH
Re: Time complexity of ThreeToTheN
We don't need no stinkin' Math class:

Code:

```public int threeToThe(int n) {   if (n < 0) return 0;   if (n == 0) return 1;   return 3*threeToThe(n-1); }```
kind regards,

Jos