17 October 2011

Problem 5 - Second Attempt

Put down a rough outline for a better solution to problem 5. As you can see, this program is more compact, however, the logic is not as easy to follow, and I trip up myself plenty why typing it out. Hopefully it works when I let it run overnight though!


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 num = 20, answer = 0, n = 1;

// while the answer has not been found
// continue searching
while (answer == 0)
{
n = 1;
// set dividor back to 1 and start again
while (n <= 20) 

{
if ((num % n) == 0) 
 n++;
}
if ((num % 20 == 0) && (n == 20)) 
{
answer = num; 
System.out.print("The answer is: " + answer); } num++; 
}
}
}


If it doesn't work, I will probably have to make some logic adjustments. Have not tried this in Mathematica yet, the if statements and while loops are very different syntactically from Java.

No comments:

Post a Comment