Tuesday, 19 November 2013

Finding Solutions to Complex Questions

In class last week we were given a question problem where we had to come up with a solution to finding the greatest possible product from the sums of a number. I decided to try and come up with a solution for this question. The first thing i did was try to  look for any kind of pattern in the first few numbers in the sequences.
starting with 1, and increasing upward the best set of sums to multiply are:
1, 1
2, 2
3, 3
4, 2 + 2
5, 3 + 2
6, 3 + 3
7, 3 + 2 + 2
8, 3 + 3 + 2
9, 3 + 3 + 3
10, 3 + 3 + 2 + 2

I began to notice something, 3 and 2 seemed to be the numbers you wanted to use for getting the highest possible product. I hypothesize this has something to do with exponential multiplication in that the more you can multiply the numbers by anything higher than 1 the better overall. To then see if this held true for larger numbers i examined 30 to see if what i thought would be the right combination would turn out ot be true.

30, 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3; product of 59049
I am not going to write out all the other possible combinations, but  i will look at a few
30, 5 + 5 + 5 + 5 + 5 + 5; product 15625
30, 10 + 10 + 10; product 1000

There appears to be a correlation between product and the number of multiplicands there are in the sum.

With this information, i thought i would try and make a racket program that could give you the  highest product for any given positive whole number.

This is what i came up with:

; prod : number -> number
; takes a positive whole number and displays the greatest possible product that can be made from the sums of the number
(check-expect (prod 2) 2)
(check-expect (prod 5) 6)
(check-expect (prod 30) 59049)
(define (prod x)
  (cond
    [(or (equal? x 1)  (equal? x 2) (equal? x 3 )) x]
    [(> x 3) (cond
               [(equal? (remainder x 3) 1) (* (expt 3 (- (quotient x 3) 1)) 4)]
               [(equal? (remainder x 3) 2) (* (expt 3 (quotient x 3)) 2)]
               [(equal? (remainder x 3) 0) (expt 3 (quotient x 3))])]))


No comments:

Post a Comment