Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Recursion Error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tewg




PostPosted: Sun Apr 13, 2008 10:13 pm   Post subject: Recursion Error

I'm having a java.lang.StackOverflowError when i run, i know its n RTP but where would my logic be off, I'm looking to find the lowest number divisable by 1, 20 inclusive.

code:


import hsa.*;
public class Prob5
{
    private static void tryit (int n)
    {
        for (int i = 1 ; i <= 20 ; i++)
        {
            if (n % i != 0)
            {
                tryit (n + 1);
            }
        }
        Stdout.print (n);

    }


    public static void main (String args[])
    {
        tryit (20);
    }
}
Sponsor
Sponsor
Sponsor
sponsor
Tewg




PostPosted: Sun Apr 13, 2008 10:16 pm   Post subject: Re: Recursion Error

ok i tryed it with the smaller numbers 1,10 inclusive which works, but stops 1 number before 2520 (answer) in a continuos loop and i can't for the life of me see why. I have put several output statements in to trace the execution but i still can't find the logic error.
Tony




PostPosted: Sun Apr 13, 2008 10:30 pm   Post subject: RE:Recursion Error

Are you sure that recursion is the best method to loop here? The lowest number would be a multiple of all the largest factors (I mean factors that are not already a part of larger factors in the list), so:

11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20

= 670 442 572 800

That's a lot to stack. Assuming an unreasonably low 1 byte per stack call, you gonna need to find a computer with 670 GB of RAM.

This will probably take a while to run as well.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Tewg




PostPosted: Sun Apr 13, 2008 10:33 pm   Post subject: Re: Recursion Error

oh I've already figured this wont work with this bg a number I'm just playing with recursion because i have little to no experience with it.
Tony




PostPosted: Sun Apr 13, 2008 10:37 pm   Post subject: Re: Recursion Error

Tewg @ Sun Apr 13, 2008 10:33 pm wrote:
i have little to no experience with it.

It's probably helpful to know how recursion is handled by the compiler/interpreter.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Tewg




PostPosted: Sun Apr 13, 2008 10:43 pm   Post subject: RE:Recursion Error

Yah, I'm just reading up on that now, thanks for your help.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: