Posted: 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
Tewg
Posted: 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
Posted: 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.