Friday, 5 April 2013

Finding Maximum Product

In continuation of the investigation we had in class this week, I've decided try to come up with a solution to find the maximum product of any given integer.

I have discovered that for any integer larger than 3, it can always be written as a combination of 2's and 3's, and for any integer larger than 4 (at 4, the max product is 4 itself), the product of a combination of these 2's and 2's is always going to be bigger than itself.

But how do you make sure that it's the largest possible product? We need the product to be the largest combination of 3's that can be made from this number. In doing so, we break it down to 2 cases:

Multiple of 3: then the product will always be 3^(multiples of 3 in number).

Else: no matter if it's an odd or even number, we need to break this number down until we find a combination of 3's. And since we know that this product is ONLY composed of 2's and 3's, the best way to do this would be to remove a 2 from this number, and try to make the rest divisible by 3.

At this point, my logic is starting to piece together into a function!

So consider the following recursive implementation of max_product:


def max_product(num):
    if num == 1:
        return 1
    
    elif num == 2:
        return 2
    
    elif num % 3 == 0:
        return 3 ** (num // 3)
    
    else:
        return 2 * max_product(num - 2)

The function will recursively combine the largest possible number of 3's with 2's to create the largest product,
so we can let the function do the rest of the work, and we're done!


Good luck to everyone on the exam! I'm really glad I have a whole week after my calculus exam to sort through everything we learned this semester.

   *'``・*。     |    `*。   ,。∩    * +  (´∀` ) *。+゜ `*。 ヽ、 つ *゜*   `・+。*・'⊃+゜   ☆  ∪~。*゜    `・+。*・

No comments:

Post a Comment