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.


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.


Tuesday 26 March 2013

Project2

I saw Tori's slog and she felt one week was too short for her because of long weekend. I thought this week would also be very short because good Friday is approaching.

Since project#2 has already been posted on our course website, professor Heap referred to it in the very beginning of the class. He explained what we should change in the project and showed us what would the image be like  if we fix them. I did not digest those information because I hasn't downloaded the code yet and did not have the chance to read the instruction. I felt I still had a lot of time to work on this project since it would be due on next week. I thought other people may also leave it until next week but I was wrong. Professor said there were some people who had already submitted this assignment. Those people finishing project so early really motivated me to some extent. I decided to look at the project and try to fix it this week.

In terms of the contents of the class, professor first showed a picture of internet map. I felt this picture looks really like fireworks display. It is beautiful. In this internet map, there are many lines with different colors and they are all meaningful. I just knew the Length of lines reveals how long a pack of information take to get in and get back through wires. I did not hear what the colors represent. After that, professor talked about the connection between machines, browsers and database. I did not quite understand when he introduced these stuff. I planned to read the long notes that he posted on the course website to figure it out.

Thursday 21 March 2013

recursion and operating system

From my perspective, recursion is easy to understand. As long as we know what will be produced if the placeholder is 0, we can use (g 0) to get (g 1) and use (g 1) to get (g 2). The first question in the recursion sheet is to produce a green outline square with the size of 10 when placeholder "d" is 0. Then, if "d" is 1, we need to use the other condition:

[(> d 0)
(overlay
(beside (g (- d 1)) (g (- d 1)))
(square (* 2 (image-width (g (- d 1)))) "outline" "green"))]

Then it will produce that two green outline squares with the size of 10 are on a big green outline square with the size of 20.


The second class in this week was an introduction of operating system and the connection between machines in a network. I felt I did not know much about the operating system like how it works and how it relates to CPU. In terms of the fixed procedures to choreograph cooperation, I learned three types of connections: roundly connected, all connected, and centrally connected. The problem in roundly connected example is that the whole network will fail if the network in one of computers fails. All connected cooperation is like human communication because someone waits for the other talks first and then he or she talks. For the centrally connected example, the sender sends out the information to the hub and the rest of receivers can get information from the hub. Only of the hub is broken, the whole network will be broken. 

I think I will be fine if the tomorrow's quiz is about recursion.

Sunday 17 March 2013

Sierpinski and Palimdrone

The first class of this week, I learned how to define "sierpinski triangle" in the Dr.Racket. However, I could not see any difference between the use of "sierp_0" and the use of "(sierp 0)". We just replace "sierp_0" with "(sierp 0)" and keep other things the same to make a definition of "(sierp 0)".

To make the picture beautiful, we could replace "green" with "(make-color (random 256) (random 256) (random 256) 255)" to let the computer choose random colors for us.

 The content of "sierpinki" won't be covered in test 2. In order to be well-prepared for test2, I did the sample test2. I was interested in the last question which is about palimdrone. I tried to figure out the definition of "palimdrone" and I made it. I thought the main idea was similar to the reverse-string which I learned in the last week. The first condition is that when the string-length of a string is less than 2, it must be a palimdrone because it is just one single character. If the string-length is greater than 2, then there will be a another condition: the first character of the string is equal to the last character of the string and the characters between the first and the last also form a "palimdrone". Then I copied the whole definition and wrote a check-expect for "palimdrone" in the Dr.Racket :
(check-expect (palindrome? "rot") false)

It passed the test! I felt so happy that I could do it on my own and succeed.

Tuesday 5 March 2013

Rot13

Last week, I learned "ROT13". When a character is between A and M, we just achieve a new character that has a position by adding 13 of it in alphabet. If the character is between N and Z, we can subtract it. Likewise, if the character is not capital but lower-case character, the basic idea is still adding 13 when the character is in-between "a" and "m" or subtracting 13 when it is in-between "n" and "z".  In today's class, we use Dr. Racket to operate the rule by using "cond." What is given is a string and what is produced is a new string that is "rotated" by 13. It is easy to write the rule of "ROT13", but how about reversing characters? This is not simple. Reverse-string needs to combine "string append,""sub-string," and even "reverse-string" itself. At first, we take the first left character and put it on the right by using "sub-string", then we can reverse the rest characters and put them together by using "string-append".  I thought we could also reverse a list having individual characters and then use "string-append" to put them together to achieve a new string. I haven't tried this method and I am not so sure about it.