Friday 5 April 2013

Problem solving : diagonals



- understanding problems:
When i draw a line from a upper left to the lower right corner (the diagonal) of the rectangle consists of square grids, i need to find the number of squares that the line crosses. For example:
the shaded squares are the ones crossed by the line drawn from the upper left to right bottom corner.


-Devise a plan
Let's say n is the number of squares in rows (horizontal) of the rectangular grid, and m is the number of squares in columns(vertical) of the rectangular grid. The example provided above shows the rectangular grid of m=4 and n=6 and the number of squares crossed by the line is 8. We can try different combinations of m and n. Then, see the pattern!!

-carry out the plan
I have tried many times and produced the chart of every different combination that ranges from m=1/n=1 to m=10/n=10. The cells in the interior, those other than upper edge of the numbers and left edge of the numbers, represent the number of squares crossed when i drawn a diagonal line from top left to the bottom right. Therefore, to illustrate by looking at the chart we can see when m=3 and n=5 the number of squares crossed is 7 and m=6 and n=7 the number of squares crossed is 12 and so forth.

        N

M
1
2
3
4
5
6
7
8
9
10
1
1
2
3
4
5
6
7
8
9
10
2
2
2
4
4
6
6
8
8
10
10
3
3
4
3
6
7
6
9
10
9
12
4
4
4
6
4
8
8
10
8
12
12
5
5
6
7
8
5
10
11
12
13
10
6
6
6
6
8
10
6
12
12
12
14
7
7
8
9
10
11
12
7
14
15
16
8
8
8
10
8
12
12
14
8
16
16
9
9
10
9
12
13
12
15
16
9
18
10
10
10
12
12
10
14
16
16
18
10

- Look back/ Acknowledge when and how, you're stuck
Ok! now we have the data let's see what pattern i can produce. Just by looking at the chart I had NO IDEA what kind of pattern would produce the table like that... (I was like -_-??)  Danny gave hint consider the cases when n=m+1 and when both n and m are even numbers. Hmm... yes, indeed, i found reasonable portion of them have the formula of : #of square crossed=m+n-1 for example (m=10 and n=9) and the even numbers did not agree to that formula... But, i noticed in the case of even numbers the line that is drawn diagonally from upper left to bottom right crosses one of the vertex of the square and there is actually two rectangular grid associated with it. For example, in the example shown in the very top

there is actually two rectangular grid of m=3 and n=2. Therefore, you subtract by 2 instead of 1 to obtain: #of squares crossed= m+n-2
However, there are still lot of exceptions how would you explain cases like m=6 and n=9? number of square crossed cannot be produced by the formula: #of square crossed= m+n-1 or #of square crossed= m+n-2. for that the answer should be 12 but each of the formula produces 14 and 13 respectively.

After playing around with numbers I have found out the final solution to this problem is that 
# of square crossed= m+n - k, where k is the greatest common factor of m and n.
For example, in the case of m=6 and n=9 the greatest common factor of both 6 and 9 is 3. Therefore, 
# of square crossed= 6+9-3
                               = 12!!
other cases like
m=2 and n=7, greatest common factor for both 2 and 7 is 1!
# of square crossed= 2+7-1
                              =8!!
m=2 and n=10, the greatest common factor for both 2 and 10 is 2.
# of square crossed = 2+10-2
                                =10!!

FINALLY, the formula for this is # of square crossed = m + n - k , where k is the greatest common factor of n and m







Sunday 31 March 2013

11th week of the course

on the 11th week of the course, the professor firstly explained what is to be done for the final assignment tat is due on friday. Then, he moved on to contents in the lecture notes and  talked about how internet works. He sent a ping to a certain website in austrailia and showed how long it takes send a response back to his computer. Also on thursday, he showed the daily time spent by human beings at which their work is most productive. He showed that after certain time has passed hour work results in negative outcome due to error, and sleeplessness is one of the biggest factor why that causes this depressing result. Our cognitive skills is ultimately limited when we do not sleep.

I also have to mention, I was going to write a slog about problem solving question so i sent Danny an email requesting some problem solving questions. However i noticed that i sent the email to the erroneous address of heap@cs.utoronto.edu instead of heap@cs.toronto.edu. Sorry for the mistake.

Monday 25 March 2013

recursion hand-out (week of 11)

Last week, I had trouble understanding the last function from the recursion hand-out. The function, i had hard time to figure out is defined as:

(require picturing-programs)

(define (s d)
  (cond
    [(> d 0)
     (beside/align 'bottom
                   (line 0 (+ 5 (* 2 d)) 'red)
                   (rotate 90 (s (- d 1))))]
  [else (line 0 5 "red")]))

I was playing around with it and started plugging numbers into the functions described above. (s 0) produced just vertical red line that is 5 pixels long. (s 1) produced two lines one laying horizontally in the bottom and the other attached vertically left to this line. Until now, i was just yawing at what functions produced. However, when i plugged (s 30) i was stunned by the result.

Initially, i had to understand where the (line 0 5 "red") comes from. I noticed that this function is derived from the Pythagorean theory; (c^2 = a^2 + b^2) where the two numbers from the function represent the vertical and horizontal component (a and b) of the line.

Now, i now know what this function is comprised of, i had to understand what the function as a whole do.

(s 0) produced one vertical line because of the condition if d is not bigger than zero or represent anything other, it just produces (line 0 5 "red").

(s 1) produced two lines as i described above. This was not surprising because from the portion (line 0 (+ 5 (* 2 d)) 'red), if we plug 1 instead of two, it results in (line 0 7 "red"). Therefore, it aligns a vertical line that is 7 pixels long with a (rotate 90 (s 0)), which is the vertical line that is 5 pixels long but rotated by 90 degrees.

(s 2) produced three lines. I was just taken aback for a moment from wondering where did the extra line came from. Then, i noticed that from the portion (rotate 90 (s (- d 1))), when i plugged the 2 into only this part, it already produced two lines!! Therefore, attaching that to the  (line 0 (+ 5 (* 2 (2))) 'red) produced three lines, two vertical and one horizontal.

Therefore i noticed that the function (s d) (where d is a number) rotates the figure produced by (s d-1) by 90 degrees and attach the vertical line to it.

Wednesday 20 March 2013

Finally over!!

It has been tough time for me for several weeks. term-tests every consecutive weeks. I have just wrote my last mid-term today! finally over! last week i have written my second term-test for this course. I hope i did well. I actually forgot to write a slog for the last week and I had to skip for the lecture on tuesday this week, because i was busy studying for the test. Well, i have just saw my marks  on markus, and noticed the poor mark received for wikipedia part 2. I expected a low mark as well, and i actually could not know how to start this project at all from the first place... oh well, i will just try my best for wikipedia part 3 and hope to gain good mark. I need to work harder on this because articles that i have chosen are ridiculous that i have no idea how to change them. Since all mid-terms are over, i will be coming to every lecture and not fall behind for the remaining of the semester, and get prepared for my final exam also, writing slogs every week.

Monday 4 March 2013

busy...

Last week was really busy week for me, but still managed to get through. Upcoming several weeks will be busy as well. Therefore, i think i need to manage my time well to do good in csc104! That is all i can say for this week's slog :(

Monday 18 February 2013

first encounter to binary numbers

late post again... my computer broke down...
On Tuesday last week we learned how to overlay the barracuda on the edge of the circle so it rotates on its tale instead of its center of its body. After that, the barracuda looked actually like a second hand of a clock!! Also, when the barracuda was pointing down the barracuda stopped rotating and produced an image stop. I actually do not know how stop sign pops up when barracuda points down. I think it is produced from this function:

(define (key-press im k)
  (cond
    [(equal? k "down") (stop-with STOP)]
    [else im]))
but still, i think i have to study more to figure out how this function works.
On Thursday of that week, we learned about binary numbers. I have seen and heard about binary numbers but never used them or convert from binary numbers to the number in ten base or the other way around. It was very interesting how all the numbers that we commonly use, could be turned into number 1s and 0s and represent same meaning. We learned how to add and subtract using binary numbers. I practiced more on subtract and add binary numbers on my own time.  However, i am really not used to using the binary numbers and again, i need more practice using it. This week is reading week and i am planning to work on Wikipedia and try to finish the second part of the assignment before i go to school again.

Monday 11 February 2013

Totally forgot

I totally forgot to write a blog for last week because it was just after the test. However, i really have nothing to write about because last Tuesday i could not attend lecture because i was extremely sick. Also we just had a test. The test was a bit harder than i thought. Therefore, i really think i should try harder to obtain higher marks!