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

Thread: Python code to Java, Incorrect answers returned in Java. Did I type something wrong?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Python code to Java, Incorrect answers returned in Java. Did I type something wrong?

    I am currently working on a recursive Modular Exponentiation class in Java and I found a way to do it in Python. I worked with Python before Java, so I am not quite sure how to do it in Java.

    My Java code is written like this:

     
    public class ModPower
    {
      public static int modPower(int x, int p, int m)
      {
        x %= m;
        int ret;
     
        if (p == 0)
        {
          ret = 1;
        }
        else if (p % 2 == 0)  // if even
        {
          ret = x * modPower(x, p-1, m);
        }
        else  // if odd
        {
          ret = modPower(x, (p/2), m);
          ret *= ret;
        }
     
        return ret % m;
     
      }
    }

    I am getting correct values if I run the python code, but incorrect ones when I run the Java code. I've been reworking it for hours and can't figure out what to do! Not to mention I can't really debug a recursive class. Please help!
    Last edited by Overa; April 5th, 2011 at 06:53 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    1. Hopefully some type casting issues.
    2. Did you declare ret somewhere? I mean it is declared and initialized in your whole program?
    3. As you are new to Java, may be you are missing main() function. Did you write main somewhere and called modPower() from there?

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    2) Yes. It is declared near the top and auto initialized to 0.
    3) The main function is in another class that is part of the package.

    Actually, I've come to a code that seems to work, except on my last example, there is a stack overflow. The code cannot handle large input at all. I can't make sense of what I can do to fix the problem.

    public class ModPower
    {
      public static int modPower(int x, int p, int m)
      {
     
       int r;
     
        if ((x < 0) || (p < 0) || (m <= 0))
        {
         r = -1;
        }
     
        if (p == 0)
        {
          r = 1;
        }
        else if (2%p == 1)  // if odd
        {
     
          r=  modPower(x, ((int)(p/2)), m); 
          r *= r;
        }
        else  // if even
        {
     
          r = x * modPower(x, p-1, m);
     
        }
     
        return r % m;
     
      }
    }


    From the main program.....

    double result = ModPower.modPower(3333, 5555, 10);
        System.out.println(result);
     
        result = ModPower.modPower(3, 5555, 10);
        System.out.println(result);
     
        result = ModPower.modPower(1024, 1024, 2);
        System.out.println(result);
     
        result = ModPower.modPower(10237, 13379, 26797);
        System.out.println(result);
      }

    Output....

    7.0
    7.0
    0.0
    java.lang.StackOverflowError
    at edu.truman.cg.ModPower.modPower(ModPower.java:90)
    at edu.truman.cg.ModPower.modPower(ModPower.java:90)
    (etc.)

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    else if (2%p == 1)  // if odd
        {
     
          r=  modPower(x, ((int)(p/2)), m); 
          r *= r;
        }
        else  // if even
        {
     
          r = x * modPower(x, p-1, m);
     
        }

    What even or odd, you want to extract by the way?
    I guess you want to see if p is odd or even??? If this then do,
    if(p%2==0){
    // Even
    }
    else{
    //Odd
    }

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    And what's your RAM size by the way? Of course when you will input too large values, your RAM will definitely let it's stack overflow as you are using recursion. If you still want to do this, increase the virtual memory size and try this.

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    About 2 G, but I don't think that is an issue........or is it?
    Last edited by Overa; April 5th, 2011 at 06:54 AM.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    Man!!! Try writing a factorial program and give it a value of 100 and run it, you'll come to know the result...

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    Why don't you try making it iterative?

  9. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    Because their assignment is here:

    Assignment 5 April 2011

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    I'll recommend you to read these:
    1
    2
    3

  11. #11
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Python code to Java, Incorrect answers returned in Java. Did I type something wr

    If you're sure the code is functioning properly, but only issue is StackOverflow, consider increasing the stack size.
    thilina's blog: How to increase the java stack size
    Only a suggestion.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. What is wrong with my code? JAVA-Eclipse
    By bespinoz in forum Loops & Control Statements
    Replies: 9
    Last Post: November 17th, 2012, 03:06 PM
  2. Java equivalent of Pythonís struct.pack?
    By thiruvadi.e in forum Java SE APIs
    Replies: 2
    Last Post: July 14th, 2010, 03:09 AM
  3. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 01:00 AM
  4. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 07:38 PM

Tags for this Thread