# Thread: Time complexity of ThreeToTheN

1. Member
Join Date
Jun 2013
Posts
62
Rep Power
0

## 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.

2. ## Re: Time complexity of ThreeToTheN

What have you tried? - Matt Gemmell

3. Member
Join Date
Jun 2013
Posts
62
Rep Power
0

## Re: Time complexity of ThreeToTheN

from what i know,

somehow got this code for 2^N

Java 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?
Last edited by Malv; 06-25-2013 at 03:15 PM.

4. Member
Join Date
Jun 2013
Posts
62
Rep Power
0

## Re: Time complexity of ThreeToTheN

I've try out codes like this

Java 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
Last edited by Malv; 06-25-2013 at 03:15 PM.

5. ## Re: Time complexity of ThreeToTheN

Use BigInteger then.

6. Member
Join Date
Jun 2013
Posts
62
Rep Power
0

## Re: Time complexity of ThreeToTheN

BigInteger belongs to Math.class

java.Math.BigInteger

7. ## Re: Time complexity of ThreeToTheN

Then implement your own pow method.

8. ## Re: Time complexity of ThreeToTheN

We don't need no stinkin' Math class:

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

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•