Wednesday, April 25, 2012

Challenge 062’s Solution

Of the three, only option 3 will work properly. Option 1 starts by checking if the first number is divisible by 2. If it is, the number is printed. The writeEven method is then run again. This process results in all of the even numbers in one’s set to be printed, but in ascending order; thus, option 1 doesn’t work. Option 2 again starts by checking if the first number is divisible by 2. However, the writeEven method is only repeated if the number is even, ending if it isn’t. This process will end as soon as an odd number is inputted; thus, option 2 will not work. Finally, option 3 starts immediately by repeating the writeEven method via recursion. With this, the last number is analyzed first. If this number is divisible by 2, it is printed. This process results in all of the even numbers in one’s set to be printed in descending order; thus, option 3 works.
 
−Phoenix

1 comment: