Tuesday, February 21, 2012

Why multiplying a fraction by 100 gives the percentage

I remember I had this curiosity about why it is that when you multiply a fraction by a number you get that fraction of that number. Why is it that 1/2 × 5 gives half of 5? Why is it that you find the percentage from a fraction by multiplying that fraction by 100? Why is it that multiplication should have this property?

You might want to look at my previous post which explains what fractions mean.

Percentages
As you should know, if for example you have a 10% income tax, that means that out of every 100 units of income you make, 10 units of them are taxes. So a percentage means the number of units you need to take out of every 100 units of a total. This is why it's called "percent", that is, "per hundred", because you are finding the amount of units you need to take our of every hundred units in the total.

However we can also express this statement using fractions instead of percentages. We can just say that 1/10 of your income is taxes. In fact we can convert fractions into percentages by multiplying the fraction by 100, 1/10 × 100 = 10, that is 10%.

Before we understand percentages, we need to understand fractions of totals.

The statement A/B of C means two things:
  1. As explained in the last post, it means divide C into B equal parts and take A such parts.
  2. It also means A is the number of units to take out of every B units in C.
For example, if we want to take 3/4 of 20,

We can either break 20 into 4 equal parts and take 3 of them:
20 = 5 + 5 + 5 + 5 (4 equal parts)
take 3 of the parts and we have
5 + 5 + 5 = 15

Or we can take 3 from every 4 in 20:
20 = 4 + 4 + 4 + 4 + 4
take 3 from every 4 and we have
3 + 3 + 3 + 3 + 3 = 15

In general, if we want to take A/B of C,
C = C/B + C/B + C/B ... (for B times) (B equal parts, that is, C/B × B which is equal to C)
take A of the parts and we have
C/B + C/B ... (for A times) = C/B × A

Or we can take A from every B in C,
C = B + B + B ... (for C/B times) (that is, B × C/B which is equal to C)
take A from every B and we have
A + A + A ... (for C/B times) = A × C/B

Since C/B × A = A × C/B, we know that the two statements are equal.

Good. So now we return to percentages. The reason why we convert fractions to percentages by multiplying the fraction by 100 is the following:
Given a fraction A/B, when we convert it to a percentage, we are changing the denominator of said fraction to 100 but leaving the fraction equal to A/B, and taking the numerator. So A/B becomes P/100 and P is the percentage.

We are finding a number which when divided by 100 gives the original fraction and therefore the amount of units you need to take from every 100 units of a total such that when you divide the amount you took by the total, you get the original fraction.

For example, if you have a total of 50 units and you want to take 1/10 of the total, the number of units you must take from the total, when divided by 50 must result in 1/10. Likewise, if we change the denominator of the fraction to 100, that is, 10/100, then we say that 10% of 50 units is the number of units we must take such that when it is divided by 50 we get 10/100 (which is equal to 1/10).

Percentages are useful because we would be standardizing the denominator of fractions in order to make them easy to compare. If we wanted to compare 2/4 to 4/16 we can change the denominators of both fractions to 100 (50/100 and 25/100 respectively) and then we will only have to compare the numerators in order to know by how much one fraction is bigger than the other.

So what we're doing is finding another fraction which is of the form P/100. However, P/100 must equal A/B in order to remain the same fraction.

So we have the equation A/B = P/100
We want to find P, so P = A/B × 100 (multiplied both sides by 100)

So if we want to express 2/4 as a percentage,
2/4 = P/100
P = 2/4 × 100
P = 50
So 2/4 = 50/100 or 50%.

I think that the percentage sign "%" can be treated as a symbol representing the constant "1/100". Which means that 50% = 50 × 1/100. This makes sense as in order to go from percentage to fraction form you just change the % back to 1/100 and calculate the expression. 50% = 50 × 1/100 = 1/2.

And this is why percentages work this way.

In general
Now we can generalize this to numbers other than 100. If we use "X" instead of "100",
A/B = P/X
P = A/B × X

By changing the denominator to X, the numerator P will be the amount of units you need to take out of every X units of a total, such that when you divide the amount you took by the total, you get A/B.

So, since P/X is equal to A/B, then just like we can say that P/X means that P is P/X of X, we can also say that P is A/B of X. For example, if 2.5/5 = 1/2, then just like 2.5 is 2.5/5 of 5, 2.5 is also 1/2 of 5.

Why does A equal A/B of B? Understanding what fractions mean.

I believe that the simplest things are often the most complex to understand, because we take them for granted and never question them. But when we do question them, we find that in their simplicity it is very hard to find simpler things into which they can be broken down to. One such simple thing is fractions. Why does the fraction 2/3 mean that 2 is 2/3 of 3? Most of you might be amazed that anyone would ask that. After all, we've been assuming that since we were very young. But challenges are what keep us sharp and what drive us to improve ourselves, so I find the challenge to understand this question inviting.

Notice that I'm assuming that a fraction is made of 2 positive whole numbers. We won't be considering irrational numbers, fractions with other fractions as numerator and denominator or even fractions with negative numbers. General fractions of these kinds will be considered in another post in the future.

Let's start from what a fraction means. 1/B means divide 1 into B equal parts and take one such part. A/B means take A such parts. So 2/5 means divide 1 into 5 equal parts and take two such parts. 6/5 means take six such parts, and so on.

In terms of fractions, "1" is called a "whole". When we divide a whole into B parts, each part is called a "Bth" (for example fourth, fifth, etc) which means 1/B. When we take A of the Bths we say that we have "A Bths" (for example one fourth, two fifths, etc) which means A/B.

Another thing the fraction A/B means is divide A into B equal parts and take one such part. Why is this definition equal to the first definition?

If we divide 1 into B equal parts, each part would be 1/B. But we want to divide A into B equal parts. Since A is A times as much as 1 (for example 2 is two times as much as 1, 3 is three times as much as 1, etc), each of its B equal parts are also A times as much as 1/B. So each part is A × 1/B. For example, if we want to find how big each part of 2 divided into 3 equal parts is, we first see how big 1 divided into 3 equal parts is, which is 1/3, then, since 2 is twice as big as 1, we double 1/3, giving 2 × 1/3.

What is A × 1/B? It's 1/B for A times, that is, A Bths, which is A/B. So we have shown that A × 1/B = A/B and that A/B means both "1 divided into B equal parts and take A parts" and "A divided into B equal parts and take 1 part". So 2/3 means both "1 divided into 3 equal parts and take 2 parts" and "2 divided into 3 equal parts and take 1 part".

Now that we have these two definitions, why does A = A/B of B? Why is it that 2 is 2/3 of 3? First, we must understand what "A/B of C" means.

What does A Bths of C mean? It means divide C into B equal parts and take A of them. Two thirds of four means divide 4 into 3 equal parts, or thirds, and take 2 of them. This implicitly means that A Bths on its own means A Bths of 1. So using our second definition of A/B, we can say that A/B of C is equal to C/B × A.

Does C/B × A equal A/B × C? We'll show this by showing that both of those expressions are equal to (A × C)/B or "the area of a C by A rectangle divided into B equal parts and taking 1 such part". The best way to understand these quantities is through a graphical representation.


The diagram on the left represents A/B × C, that is, a length A divided into B equal parts, extended into a rectangle C long and take one such part. The diagram on the right represents C/B × A, that is, a length C divided into B equal parts, extended into a rectangle A long and take one such part.

Since both rectangles are A by C with the difference that one is a rotated version of the other, and both are divided into B equal parts, we can save that both rectangles are equal to A × C and we are taking one Bth of such a rectangle. So C/B × A = A/B × C = (A × C)/B.

Great, so now we can say that A/B of C is equal to A/B × C, which means that we can just replace the "of" with a "×". So now we have a good understanding of what A/B of C means. Now we move to why A is A/B of B.

What does "A/B of B" mean? It means "divide B into B equal parts and take A such parts" or B/B × A. What is a Bth of B? It is 1/B × B. 1 divided into B equal parts and take B such parts. But then you would be taking all the parts which form 1 again. So a Bth of B is 1, therefore B/B = 1 (unless B is 0 in which case dividing 1 into 0 equal parts will not make sense). So B/B × A = 1 × A which we know equals A.

Great! So now we know that A/B of B is A, that A/B of C means A/B × C, that A/B × C = C/B × A = (A × C)/B and that A/B means both "1 divided into B equal parts and take A such parts" and "A divided into B equal parts and take 1 such part". Next we'll see how these are applied to percentages.

Monday, November 7, 2011

A more complete proof that the square root of 2 is irrational.

I don't know about you but I never quite liked the usual proof by contradiction that the square root of 2 is irrational. It seems incomplete in some way. I never felt convinced by it. Here's what I feel is the missing piece of the puzzle.

Assume that the square root of 2 is rational. So,
√2 = a/b
=>
2 = (a/b)^2
=>
2 = a^2 / b^2
=>
2 b^2 = a^2

So far so good. The usual proof continues with the following statement:

Since a^2 is equal to a natural number multiplied by 2, a^2 is an even number. But for a^2 to be even, a must be even too (see proof in the appendix at the end). So that means that there is a natural number k where a = 2k.

Since a = 2k and 2 b^2 = a^2,
2 b^2 = a^2
=>
2 b^2 = (2k)^2
=>
2 b^2 = 4 k^2
=>
b^2 = 2 k^2

Just like for a, b must also be an even number.

The proof usually ends right there, claiming that since a and b are both even numbers, then the fraction a/b is not simplified and irreducible, contradicting that a/b exists. But let's see where the proof takes us if we just keep on going.

If both a and b are even, then the fraction a/b can be simplified by dividing both a and b by 2, that is, if a = 2k and b = 2l, then we can say that √2 = k/l. But after doing this we can reapply the same reasoning on k and l and we'll discover that k and l are also both even numbers, and we can do it again and again ad infinitum.

So, which natural numbers can be divided by 2 infinitely? Only 1 number can do that, zero. But replacing zero for both a and b will not make their quotient a real number, or if you want to define 0/0, it will not result in a number whose square equals 2. So there is no fraction a/b which gives √2.

So there you have it, a proof that goes on till the end.

===========================
APPENDIX
Now on to the proof that an even square can only come from an even number squared:

Let a^2 be an even number.

a can either be even or odd, that is there must exist an n where
a = 2n or a = 2n + 1
If a = 2n, a^2 = (2n)^2 = 4 n^2 = 2(2 n^2), which is an even number
If a = 2n + 1, a^2 = (2n + 1)^2 = 4 n^2 + 4n + 1 = 2(2 n^2 + 2n) + 1, which is an odd number

So an even number squared will give an even number and an odd number squares will give an odd number. Hence, a square even number can only come from an even number squared.

Wednesday, November 2, 2011

Wisdom hierarchy vs Bloom's taxonomy

So lately I've been reading about two subjects that I noticed are very related, the Wisdom hierarchy ( http://www.systems-thinking.org/dikw/dikw.htm) and Bloom's taxonomy (http://www.odu.edu/educ/roverbau/Bloom/blooms_taxonomy.htm). First I need to explain each.

Wisdom hierarchy
This is a hierarchy of how wisdom is obtained and describes the relationship between data, information, knowledge, understanding and finally wisdom.

Data
Data is symbols and signals which can be observed and analysed, but perhaps not be processed and organized.
An example of this is seeing the symbols "3", "×", "4", "=" and "12". Those symbols may not mean anything to you if you don't know arithmetic.

Information
Information is data which is given meaning and use. It answers "what", "where", "who" and "when" questions, that is, simple shallow questions. It is when relationships are formed between the different data and context is given to the data. The data has meaning but perhaps it cannot be used.
So now "3×4=12" has a meaning. It means that if you multiply the numbers 3 and 4, the result is equal to 12. You may know what the symbols mean but you may not be in a form that is useful.

Knowledge
Knowledge is a mass of information which is organized in a way to be useful. It answers "how" questions, that is how can I use the information. The information may be useful but perhaps you don't understand why it is related and how to generate new information from it.
So now we have organized every multiplication of two numbers we learned into a multiplication table. If we want to know what a particular multiplication equals, we know how to do that, we simply look it up our multiplication table. You may know how to multiply numbers together but you may not know why when numbers are multiplied they give a particular number as a result.

Understanding
Understanding is when you understand the knowledge, when you find a pattern to the organization and can use the pattern to generate new information. It answers "why" questions, that is, why is the information organized as it is in the knowledge. The knowledge may be understood but perhaps it cannot be judged and compared with other knowledge.
So now we understand that multiplication is repeated addition. Now we can add to our knowledge new information which is generated from our understanding rather than from the external world (such as having to ask someone). You may understand how to do multiplication but you may not be able to compare different methods to doing multiplication.

Wisdom
Wisdom is when you can pass judgement and make decisions to determine what is the best method to use. The question it could answer is "which" questions, that is, which is best.
We now can decide which method we should use to multiply two numbers, be it by looking up the multiplication table, by repeated addition or by long multiplication.

Bloom's taxonomy
This is a way of categorizing exam questions in a hierarchy such that as you go up the pyramid, the higher the level of thought required to answer the question.

Remembering
Remembering type questions are those that only require the student to remember things, without expecting any understanding.
An example question would be "What does the symbol × represent?".

Understanding
Understanding type questions are those that require the student to know what the things they know actually mean.
An example question would be "Explain what the expression 2×3=6 means in your own words.".

Applying
Applying type questions are those that require the student to be able to use what they know in a situation.
An example question would be "How many apples would you have if you had 2 baskets with 3 apples in each?".

Analyzing
Analyzing type questions are those that require the student to break down a problem into parts and see how they are related to each other.
An example question would be "What is the next number in the sequence 21, 42, 63, __".

Evaluating
Evaluating type questions are those that require the student to justify a decision.
An example question would be "Which multiplication method would you use to multiply 128 by 64 and why?".

Creating
Creating type questions are those that require the student to create something new to the student.
An example question would be "If all you have is the product of the sum and difference of two numbers and one of the numbers, how can you find the other number?".

Together
It is clear that there is a relationship between the two hierarchies. We could say that:
Remembering type questions test the student having memorized data.
Understanding type questions test if the student has derived information from data.
Applying type questions test if the student has developed a useful knowledge from the information and if the knowledge can be readily used.
Analysis type questions test if the student has understood the basis of their knowledge and can derive new information from it.
Evaluating type questions test if the student has obtained any wisdom on the subject and hence can make sound judgement about it.

The last question type, creating, is not covered by the Wisdom hierarchy and perhaps it predicts yet another higher level form of cognition, perhaps called "creativity", which is when you use knowledge, understanding and wisdom together to derive new knowledge, understanding and wisdom, where knowledge provides the raw material to act on, understanding provides the ways to rearrange the knowledge and wisdom guides you into choosing a solution path which is most likely to give good results. Once this is done you will have learned from experience and would have added new knowledge, a deeper understanding of that knowledge together with new ways of using it and you would be able to make better judgement in the future.

Monday, September 19, 2011

A lousy tutorial to C# drag and drop

Drag and drop is a neat way to allow the user the "transfer" data into a winform control. Here's how to enable drag and drop in a windows form in C#:

1. Create your source and target controls. In this case we're using 2 ListViews where the one on the right (called listView1) is the source of the drag and the one on the left (called listView2) is target of the drop. However note that the source of the drag can even be from the windows explorer by drag dropping a file into the control.

2. Set the AllowDrop property of the control which will receive the drop to true.

3. Set up an event in the control which will be dragged from, that senses that a drag has been initiated, such as the ItemDrag event or the DragLeave event and call the DoDragDrop method. The DoDragDrop method is to be passed the data to be transferred (the object you want the receiving control to get).

4. Set up the DragOver event in the control which will be dropped on, to check if the data being dragged over can be accepted and change the cursor to an invalid cursor picture if not. You can check the type of the data being dragged by using the GetDataPresent method.

5. Set up the DragDrop event in the control which will be dropped on, to actually do something with the data once it has been dropped. This event will only fire if the DragOver event did not set Effect to None.

And there you have it. A lousy tutorial. However, here's something worth mentioning:

If you are transferring an object which could be one of several types and you want to view the object as its base type, then this will not work, as the GetData method will just return null if you pass it a base class for a type. The only was I found to get around this was by created a proxy object which will contain the object of interest casted as its base class, and then what you receive the proxy object in the receiving control you just get the object of interest from it. Like so:

Sunday, July 17, 2011

Quicksort partitioning

During my years in school and at university I was always exposed to only one algorithm of quicksort partitioning. Let's take a look at some ways to partition a list for sorting.

Quicksort itself works by taking a list, picking an element from the list which is referred to as the "pivot" and splitting the list into two sub lists, the first containing all the elements smaller than the pivot and the second containing all the elements greater than the pivot. Once you have these two sub lists you can sort each one independently of the other since elements in one list will not be moved into the other after the sort is complete.

This splitting into two lists is called "partitioning". Partitioning once will not sort the list but it will allow you to either use a different sorting algorithm on each sub list (partition) or to recursively partition the two partitions until you end up with a partition of 1 or 0 elements, which is necessarily sorted.

For example, partitioning the list [7,3,6,4,1,7,3] using 4 as a pivot will give us a first partition of [3,1,3] and a second partition of [7,6,7]. The pivot itself, along with other duplicates of it, may or may not go in one of the partitions, depending on how the partitioning is done. If it does not go into one of the partitions, then the sort will place the pivot between the 2 partitions after they have been sorted.

Partitioning by filtering

The most intuitive way to partition is by creating 2 new lists, going through the unsorted list and copying elements from the unsorted list into one of the 2 lists. This is memory expensive however as you end up needing twice as much space as the unsorted list takes. The following partitioning algorithms are "in-place" and hence do not need any new lists.

Partitioning by moving the pivot

This is the partitioning algorithm I was familiar with at school. It's quite intuitive but slow when compared to the next algorithm. The way this works is by putting the pivot into its sorted place, that is, the place where it will be after the whole list has been sorted. All the elements smaller than the pivot will be on its left and all the elements larger than the pivot will be on its right. Therefore you would have created 2 partitions, the left side of the pivot and the right side.

The algorithm uses a pivot pointer which keeps track of where the pivot is and an index pointer which is used to compare the pivot to other elements. The pivot pointer starts by being at the right end of the list (you can choose a pivot and swap it with the last element if you don't want to stick to the element which happens to be there) and the index pointer starts by being at the left end of the list. The index moves towards the pivot pointer until it encounters an element which is not on the correct side of the pivot, upon which the index and the pivot and swapped and the the index pointer and pivot pointer swap locations. Once the index pointer and pivot pointer meet, the pivot is in its sorted location and the left and right side of the pivot are partitions.

Pseudo code:
function partition(arr, left, right)
  pivotPtr = right
  indexPtr = left
  while pivotPtr != indexPtr
    if indexPtr < pivotPtr //if index pointer is to the left of the pivot
      while arr[indexPtr] <= arr[pivotPtr] and indexPtr < pivotPtr
        indexPtr++ //move index pointer towards the pivot
      if indexPtr < pivotPtr
        swap(arr[indexPtr], arr[pivotPtr])
        swap(indexPtr, pivotPtr)
    else //if index pointer is to the right of the pivot
      while arr[indexPtr] >= arr[pivotPtr] and indexPtr > pivotPtr
        indexPtr-- //move index pointer towards the pivot
      if indexPtr > pivotPtr
        swap(arr[pivotPtr], arr[indexPtr])
        swap(pivotPtr, indexPtr)
  return pivotPtr

Partitioning by dividing

In the previous partitioning algorithm, we had to constantly swap the pivot in order to eventually put it in its place. This is however unnecessary as partitioning does not require the pivot to be in its sorted place, only that we have 2 partitions, even if the pivot itself is in one of the partitions (it doesn't matter in which one as it could be eventually placed in its sorted place in either partition).

This time we will not care where the pivot is, as long as we know its value. We will need 2 pointers, a high and a low pointer, which will be moving towards each other. The low pointer will expect to encounter only elements which are smaller than the pivot and the high point will expect to encounter only elements which are larger than the pivot. When both pointers encounter a wrong element, they swap the elements and continue moving towards each other. When they eventually meet, all the elements to the left of the meeting point will be smaller than or equal to the pivot and all the elements to the right of the meeting point will be greater than or equal to the pivot.

Since both pointers will be moving toward each other before swapping, this algorithm will do less swaps than the previous one and hence will be much faster. In fact a simple experiment will show that it does half the number of swaps.


Pseudo code:
function partition(arr, left, right, pivot)
  lo = left
  hi = right
  while lo < hi
    while arr[lo] <= pivot and lo < hi
      lo++ //move low pointer towards the high pointer
    while arr[hi] >= pivot and hi > lo
      hi-- //move high pointer towards the low pointer
    if lo < hi
      swap(arr[lo], arr[hi])
  /*
    Since the high pointer moves last, the meeting point should be on an element that is greater than the pivot, that is, the meeting point marks the start of the second partition.
    However if the pivot happens to be the maximum element, the meeting point will simply be the last element and hence will not have any significant meaning.
    Therefore we need to make sure that the returned meeting point is where the starting point of the second partition is, including if the second partition is empty.
  */
  if arr[lo] < pivot
    return lo+1
  else
    return lo

Thursday, June 2, 2011

Negation Introduction

When I was in university, one of my favorite units was Mathematics of Discrete Structures lectured by Dr Gordon Pace. In propositional logic, we learned about the negation introduction rule of inference which was this:

P => Q, P => ~Q
---------------
     ~P

and which was used like this:

1.  | P   Assumed
... | ...
10. | Q

11. | P   Assumed
... | ...
20. | ~Q
21. ~P   Negation introduction from lines 1-10 and 11-20

In other words, if P implies that Q is true and also P implies that Q is not true, then P must itself not be true.

I never actually understood the logic behind it however. We were told that this how a proof by contradiction works. An example of a proof by contradiction, in words, is this:
If it were raining, the streets would be wet. But the streets are not wet. Therefore it is not raining.

The fact that if a particular proposition is true, it would lead to another proposition which we know is false, implies that the first proposition must itself be false.

The problem with the rule of inference we used for negation introduction is that the way I interpret it linguistically is not at all how a proof by contradiction is usually told. If we had to turn the rule of inference into words, it would be this:
If P implies that something is both true and false, then P is itself false.

A rule of inference which is closer to a proof by contradiction would be this:

Q, P => ~Q
----------
    ~P

which would be used like this:

...
10. Q
11. | P    Assumed
... | ...
20. | ~Q
21. ~P     Negation introduction from lines 10 and 11-20

This is closer to a proof by contradiction.
If P implies something which is not true, then P itself is not true.

The question is, can we use this new rule of inference instead of the usual one?

When can the usual rule of inference be more powerful than the proposed one? Only when both Q and ~Q can ONLY be derived using P. Hence P must be assumed in both cases which cannot be done with the proposed rule of inference.

But this can easily be solved by using the lemma Q /\ ~Q => X and tautologies. If P can derive both Q and ~Q, then we can also derive Q /\ ~Q from which further derive anything we want, including something false. Worst case, if we don't know what to derive which is false, we can derive the negation of a tautology, such as ~(A => A). A => A is a tautology and we can always just insert it in a proof.

So using this trick, we'd do the following:

1.  | P          Assumed
... | ...
10. | Q
... | ...
20. | ~Q
21. | Q /\ ~Q    Conjunction introduction from lines 10 and 20
22. | ~(A => A)  Q /\ ~Q => X lemma from line 21
23. A => A       A => A lemma
24. ~P           Negation introduction from lines 1-22 and 23

And therefore, where ever we can use the usual rule of inference, we can also use the proposed rule of inference.

QED :)