Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Time complexity of ThreeToTheN

  1. #1
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default 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. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Time complexity of ThreeToTheN

    What have you tried? - Matt Gemmell

    Post your code and ask specific questions. Nobody will do your homework for you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default 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. #4
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default 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. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Time complexity of ThreeToTheN

    Use BigInteger then.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default Re: Time complexity of ThreeToTheN

    BigInteger belongs to Math.class

    java.Math.BigInteger

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Time complexity of ThreeToTheN

    Then implement your own pow method.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default 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
    PhHein likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to write the time complexity about n!
    By hjxlpp in forum New To Java
    Replies: 1
    Last Post: 06-23-2013, 10:05 AM
  2. time complexity of toArray
    By marcosol in forum New To Java
    Replies: 13
    Last Post: 09-19-2012, 06:51 PM
  3. Time complexity question
    By romavolman in forum Advanced Java
    Replies: 8
    Last Post: 09-19-2012, 06:41 PM
  4. I need help with Time Complexity???
    By lulzim in forum Advanced Java
    Replies: 2
    Last Post: 09-20-2011, 09:11 AM
  5. I need help for Time Complexity of this???
    By lulzim in forum Advanced Java
    Replies: 9
    Last Post: 09-16-2011, 03:51 PM

Posting Permissions

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