Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with Recursive code

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Problem with Recursive code

    Hi ya folks, I''v come to this forums a few times and have found solutions to my problems, however in this instance I can not find a solution to my problem.
    Here is what I have to do:
    Write a Boolean recursive method called powerof3 that takes a single positive integer argument and returns true iff the integer is a perfect power of 3 such as 1, 3, 9, 27, 81, ...
    For example:
    if (powerof3(81))
    System.out.println("81 is a power of 3.");
    else
    System.out.println("81 is not a power of 3.";
    displays 81 is a power of 3
    The problem is that if the answer is false I run in to a Stack Over Flow error. For example, if the input is 23, I run in to an error, however if the input is 81 it passes (returns true correctly).

    Here is my code:
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int num = 243;
            boolean it3 = powerOfThree1(num);
            System.out.println(it3);
        }
     
        private static boolean powerOfThree1(int num) {
            boolean n = false;
            if (num == 1) {
                n = true;
                return true;
            }
            if (num != 1) {
               n= powerOfThree1(num / 3);
     
            }
     
            return n;
        }
    }

    Can any one tell me how I should go about solving this? Also, is there any other method that I can use?

    One more thing: I can not use Arrays, ArrayLists,etc for this assignment.

    PS: I'm more of a hardware guy (modder, OCer,etc) than a programmer and I am finding recursions to be a bit difficult to understand and follow. Any tutorials, guides,etc will be helpful.
    Last edited by Shadow703793; February 22nd, 2010 at 09:38 PM. Reason: solved


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Problem with Recursive code

    Check to see if num is below 1, if it is then you know that your number is not a power of 3. Also, by my judgement your program will consider 82 to be a power of 3, which as you well know is 1 more than 81 which is a power of three so 82 cannot be a power of three. You may wish to sort this out. I'll give you a massive clue

    82/3 != 27

    Regards,
    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Shadow703793 (February 21st, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Recursive code

    Thank you. I fixed the code for Stack Over Flow as you suggested. However, I can't figure out how to fix the problem of 82/3 !=27, the reason being is that this happens to 28,10,etc. so basically at every num +1. I tried Math.ceil() but that didn't work.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with Recursive code

    Remember, integers automatically truncate any decimals when you divide. Use the modulus operator to see if the number is still divisible by 3 before dividing it by 3.

  6. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Recursive code

    ^ Got it thanks!

    edit: Can a mod please mark this as [Solved] I can't seem to do this in my OP.
    Last edited by Shadow703793; March 3rd, 2010 at 09:04 PM.

Similar Threads

  1. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM
  2. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  3. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  4. recursive search of all local disks
    By ttsdinesh in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 08:23 AM
  5. Recursive Solution to Knights tour
    By budder8818 in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 4th, 2009, 03:31 PM

Tags for this Thread