Friday 5 April 2013

Products of Sums

I was trying to work this through recursion, I'm not exactly sure if I got it right. but I think I'm on the right track, since it seems that the greatest sum usually consists of a 3 and/or 2's.


def great_mult(num):
    lists = []
   
    if num <= 3:
        lists.append(num)
        return lists
   
    if num % 3 == 0:
        num = (num - 3)
        lists.append(3)
        lists.extend(great_mult(num))
    else:
        num = (num - 2)
        lists.append(2)
        lists.extend(great_mult(num))
       
    return lists

It's nice to finally have some time to try the problems shown in class.

2 comments:

  1. Wow it returns a list of the numbers! It seems right. I made a program to calculate the products and they match! :)

    ReplyDelete
  2. Ya I wasn't sure if it would work for everything, since I only did a few examples and saw a vague pattern :P

    ReplyDelete