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 7 of 7

Thread: Rewriting a method recursivly

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Rewriting a method recursivly

    Hello!

    Consider this code

    public static boolean isPower(int x, int n) {
     
        if (x == 1)
            return (n == 1);
     
        int pow = 1;
        while (pow < n)
            pow = pow * x;
     
     
        return (pow == n);
    }

    The goal is to find if the number x is a power of the number n.I've come up with this algorithm,and it works.I'd like to solve it recursivly as well,but I cannot come up with a good idea to start.

    Thanks for the replies.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Rewriting a method recursivly

    Can you use integer division in the problem? If so, there is a much simpler solution.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rewriting a method recursivly

    Quote Originally Posted by Norm View Post
    Can you use integer division in the problem? If so, there is a much simpler solution.
    Actually I've solved it already,like this.I dont need to solve it recursivly I want to solve it recursivly.To be precise I want to be able to "transform" iterative code into recursive,to figure out how to do that.

    If integer division is part of that recursive solution than sure,anything is allowed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Rewriting a method recursivly

    I don't see how to do it without passing x, n and pow to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rewriting a method recursivly

    Quote Originally Posted by Norm View Post
    I don't see how to do it without passing x, n and pow to the method.
    I found online how to transform iteration into recursion,followed the guide (its literally a pattern you have to follow).And I solved the problem.For those are interested;

    public static boolean isPower_recursive(int x,int n) {
        if(x == 1)
            return (n==1);
        return isPower_recursion( x, n, 1);
     
        }
        public static boolean isPower_recursion(int x,int n, int pow) {
        if( pow > n) {
            return (pow == n);
        }
        pow = pow * x;
        return isPower_recursion(x,n,pow);
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Rewriting a method recursivly

    Thanks for posting your solution. I see that the code passes 3 variables to the recursive method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rewriting a method recursivly

    Quote Originally Posted by Norm View Post
    Thanks for posting your solution. I see that the code passes 3 variables to the recursive method.
    Yea,also a little typo from me.

    public class test {
    	public static void main(String[] args) {
    		System.out.println(isPower_recursive(2,16));
     
     
    }
     
    public static boolean isPower(int x, int n) {
            if (x == 1)//header
                return (n == 1);//header
     
     
            int pow = 1;//header
            while (pow < n) {//loop
                pow = pow * x;// loop body
     
    		}
            return (pow == n); //return statement
        }
     
    	public static boolean isPower_recursive(int x,int n) {
        if(x == 1)
            return (n==1);
        return isPower_recursion( x, n, 1);
     
        }
        public static boolean isPower_recursion(int x,int n, int pow) {
        if( pow >= n) {
            return (pow == n);
        }
        pow = pow * x;
        return isPower_recursion(x,n,pow);
     
    }
    }

    Its >= not >.

Similar Threads

  1. Lots of errors after rewriting an existing program
    By k1fu in forum Computer Support
    Replies: 1
    Last Post: October 30th, 2020, 01:03 PM
  2. Replies: 3
    Last Post: March 30th, 2019, 09:06 AM
  3. Url rewriting related..
    By sk007 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2013, 07:37 AM
  4. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  5. Help Please - Rewriting text
    By dechno in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 21st, 2011, 03:00 PM