19 October 2011

Problem 5 - Second Attempt (revisit)

So I decided to sift through my logic for Problem 5 and made some minor corrections, changed some of the variables, and added a few println's so that I could make sure the program was going into the necessary loops and so that I can see if the output is coming along nicely, which it seems to be doing well. 


I have the program running in the background right now, and it certainly does not conform to the 1-minute rule, meaning it should be able to run in one minute, but if I get the correct answer, I will be happy. I will paste the code below, before I got over how I think I can get it to run faster.
PasteBin Link 


public class Problem_5
{

public static void main(String[] args)
{
// num is what will look for answer
// answer will be set to num when found
// n is for recursively dividing
int count = 20, ans = 0, n = 2;

// while the answer has not been found
// continue searching
while (ans == 0)
{
//System.out.println("Entered loop");
n = 2;
// set dividor back to 2 and start again
while (n <= 20) 


if ((count%n) == 0) 

System.out.println("" + count + " " + n); 
n++; 

else 
n = 21; } 
// if num can be modded by 20 
// and if n gets to 20 
// then this must be the answer 
// otherwise the while loop 
// would have stopped 
if ((count % 20 == 0) && (n == 20)) 

ans = count; 
System.out.println("The answer is: " + ans); 

count++; 
} } }


All of the possible answers are even, therefore I could just add two for the counter, and that may cut the run time by roughly half, but I know that will not be enough because I started the program 14 minutes ago, and it is still running

No comments:

Post a Comment