Thursday, 4 April 2013

Products of Sum

I like math questions so I looked at the problem for "Products of Sum". It was really interesting and evoked my thinking. In order to find the solutions, I followed the instructions of of "G. Polya, How to Solve It"

1 UNDERSTANDING THE PROBLEM
The first thing I need to do is to understand the questions and know what I am supposed to do to solve it.
For "Product of Sum", I need to find the maximum product in a list of positive integers that sum to n if n is a
positive integer. If n is 2, there are two lists: (1, 1) and (2) product of (1,1) is 1 and product of (2) is just 2 so the maximum product is 2. Therefore, I need to test many times and find the pattern.

2 DEVISING A PLAN
In order to find the pattern, I test numbers from 1 to 15:

1 : just 1 max product is 1
2 : (1,1), or (2) max product is 2
3 : (1,1,1) or (1,2) or (3) max product is 3
4 : (1,1,1,1),(1,1,2),(1,3), (4) max product is 4
5 : (1,1,1,1,1), (1,1,1,2), (1,1,3),(1,2,2),(2,3) max product is 6
6 : (1,1,1,1,1,1), (1,1,1,1,2), (1,1,1,3),(1,1,4),(2,1,3),(2,2,2),(3,3) max product is 9
7 : .... (3,2,2) max product
8 : .... (3,3,2) max product is 18
9 : .... (3,3,3) max product is 27
10: .... (3,3,2,2) max product is 36
11: .... (3,3,2,3) max product is 54
12: .... (3,3,3,3) max product is 81
13: .... (3,3,3,2,2) max product is 108
14: .... (3,3,3,3,2) max product is 162
15: .... (3,3,3,3,3) max product is 243
By carefully looking at the pattern, I find:
when the numbers are less than 4, the maximum product is just the number itself
when the numbers are greater than 4, the maximum product is achieved by using factors numbers 2, 3, or,4. Then, I tested number 10:
If I divide 10 by 2, it is 2*5, I can write the list :(2 2 2 2 2) or (5 5) and their products are 32 and 25 respectively.
if I divide 10 by 3, it is 3*3+1, so I can write its factor numbers: 3, 3, 2,2. The product of them is definitely greater than 32 and 25.
If I divide 10 by 4, it is 2*4+2, so the list is (4 4 2) and the product is 32.
I also test other numbers and I find the maximum product is by dividing the number by 3 and the remainder is less than 3.


3 CARRYING OUT THE PLAN
when n=1, 2, 3,and 4, the maximum of product is itself.
When n is greater than 4.

Suppose n/3=m+remainder
If there is no remainder, I can write a list of 3s and there are m*3s. Then the maximum product is 3^m.
If the remainder is 1, I can write a list of (m-1)*3s and (3+1), Then the maximum product is 3^(m-1)*4
If the remainder is 2, the maximum product is 3^m*2


4 LOOK BACK
I check my results by predicting number 16. 16/3=3*5+1
so the remainder is 1,the maximum product is 3^4*4=324
Then, I test the number 16 and I find it is true.


Through this experience, I think I can work very efficiently when I follow these instructions. I will use these steps when solving other math  problems.



The last week

The first class in this week was about privacy. Beginning with this topic, professor Heap asked us: "how many people would like to share their personal information?" I thought a small fraction of people in class would raise their hands.
Two extreme situation: 1 completely keep privacy, 2 share all the information in public
I thought we should combine them together and learn to control important personal information.

Professor said that we could draw a diagram for the information we shared with different people. The center is our closed friends and relatives, a circle around the center is acquaintances, and the biggest circle is the rest of the world. I agreed it but my diagram was different. My parents will be in the center, my relatives and very closed friends are in the second circle, following another big circle of other friends and classmates. The last circle will be strangers.

When he talked about privacy leaks, I did not know very much about them such as "buyer loyalty plans", "911","rfid","recorded viewing", and "black boxes". I felt new about it.

Then I downloaded the tutorial handout. At first, I did not quite understand what is the difference when running (list 1 2 3) and (list 1 2 (list 3)) since their results are the same (list 1 2 3). Then, I followed the steps to see how it functions and I figured it out. They are both list, so it uses the first function: (apply append (map flatten L))

(map flatten L) flattens each value. (flatten 1) is (list 1) since 1 is not a list so it satisfies the second condition, which is (else list). Similarly, (flatten 2) is (list 2) and (flatten 3) is (list 3). However, (flatten (list 3)) is different though the result is the same. (list 3) is a list so it needs to use the first condition: (apply append (map flatten 3)) and the result is (list 3)
so their results are the same, which is (list 1 2 3).

I would talk to my TA tomorrow to confirm my idea. Hope I could do well in the quiz.

The Contrast

I finally finished my project 2 and I was so happy. I remembered there were some challenges I faced when I did the contrast, for example, what did the key "z" do? or how to run it?

First, I tried to understand what changes I need to make and what should be fixed in this project. Then, I looked the whole programming over very carefully and I knew how the contrast works: it requires you to increase the contrast of the red, green, blue portion of the image blinky by pressing different keys. If you press "r", it will increase the contrast the red color of blinky. If you press "g", it will increase the contrast of green color of blinky. By increasing the contrast, you should define "sharpen-r","sharpen-g"and "sharpen-b".some definition of them is wrong and what I need to do is to fix the definition. some have the right definition but do not have check expect and I need to add some check-expect. Also, write the key-press to let it run by pressing these keys.

As you can see below, Sharpen-r just changes color-red and keeps other color the same. For the change, 
it compares color-red with 128, if it is less than 128, then times 7 of the color-red and plus 0, but if it is greater than 128, then times 7 color-red and plus 255. After that, divide 8.
(define (sharpen-r c)
  (make-color
   (quotient (+ (* 7 (color-red c))
                (cond [(< (color-red c) 128) 0] [else 255])) 8)
   (color-green c) (color-blue c) (color-alpha c)))
By imitating, I just changed sharpen-r to sharpen-g and put color-red in the first position, then the sharpen-g of color-green, following color-blue and color-alpha.
Check-expect is the same idea, so I just changed the order and the name of the contrast color.
In terms of key-press, it is complicated, but if I write a right definition for one key-press, then the others would be the same idea. For example, there is a check-expect of pressing "r"
(check-expect (toggle (make-imgpair blinky blinky) "r")
              (make-imgpair blinky (map-image sharpen-r blinky)))
 I found it just increased the contrast of red portion of "imgpair after" and the "imgpair before" was still the original blinky. so according to the check-expect, I could get the definition: 
[(equal? k "r") (map-image sharpen-r (imgpair-after ip))]

I ran the system, but one test about pressing "z" failed. Then I looked it back. I found that "z" is to restore the image, which means if you press "z", it should be the same image as before. Then I fixed it and finally all my tests passed.