Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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


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