Blog    Notes   

Recent Additions

  1. A blog about puzzles, mathematics, technology, etc.
  2. cotpi puzzles
  3. Music: Flowers
  4. Music: Waiting for dawn
  5. Project: Vimtab for Windows
  6. Project: Brainfuck compiler
  7. Project: Brainfuck interpreter

All the blog posts that were previously available at blog.susam.in (running on blogger.com) have recently been moved to http://susam.in/blog/ that is running on a self-written blogging application.

The migration wasn't perfect. The HTML markup may be broken at some places. These issues would be fixed soon. If you find anything broken, please report it to susam@susam.in.

Latest post

Tuesday, November 22, 2011

Last night, I came across a simple propositional logic problem concerned with the validity of the following statement:

((A ⇒ B) ∧ (B ⇒ C)) ⇔ (A ⇒ C)

At first, I thought it is valid. But on drawing the truth table, I was surprised to see that it isn't valid.

ABC A ⇒ BB ⇒ CA ⇒ C (A ⇒ B) ∧ (B ⇒ C) ((A ⇒ B) ∧ (B ⇒ C)) ⇔ (A ⇒ C)
TTT TTT T T
TTF TFF F T
TFT FTT F F
TFF FTF F T
FTT TTT T T
FTF TFT F F
FFT TTT T T
FFF TTT T T

My intuition had failed. The truth table shows that there are two cases in which the statement is false.

Case 1: (A ⇒ C) ∧ (B ⇒ C) ∧ ¬(A ⇒ B)

In this case, A implies C and B implies C, but A does not imply B. This corresponds to the third row in the truth table. It makes sense. Two things can imply a third thing but they need not imply each other. For example, rain may imply wet soil and snow may imply wet soil as well, but rain need not imply snow. Here is another example that demonstrates this.

The following two statements are true for any integer x.

4 | x ⇒ 2 | x
6 | x ⇒ 2 | x

But the following statement is false when x ≡ 4 (mod 12) or x ≡ 8 (mod 12).

4 | x ⇒ 6 | x

In other words, if an integer is a multiple of 4 or 6, then it is also a multiple of 2. But this is not equivalent to saying that if an integer is a multiple of 4, then it is also a multiple of 6. That's not even correct as we can confirm for x ∈ {4, 8, 16, 20, 28, 32, …}.

Case 2: (A ⇒ B) ∧ (A ⇒ C) ∧ ¬(B ⇒ C)

In this case, A implies B as well as C but B does not imply C. This corresponds to the sixth row in the truth table. It makes sense as well. One thing can imply two other things but those two things need not imply each other. For example, rain may imply wet soil and flooded rivers but wet soil need not imply flooded rivers. The soil could be wet due to irrigation sprinklers. Once again, here is a mathematical example to demonstrate this.

The following two statements are true for any integer x.

6 | x ⇒ 2 | x
6 | x ⇒ 3 | x

But the following statement is false when x is an odd multiple of 3.

2 | x ⇒ 3 | x

The truth table correctly tells us that saying, "If an integer is a mutliple of 6, then it is a multiple of 2 as well as 3," is not equivalent to saying, "If an integer is a multiple of 2, it is a multiple of 3 as well." That would be nonsense.

Encoding intuition correctly

My intuition incorrectly concluded that the (A ⇒ B) ∧ (B ⇒ C) ⇔ (A ⇒ C) is valid because I was not translating the given statement correctly in my mind. I was thinking that if A implies B and B implies C, of course A must also imply C. This is true but this is not what the given statement represents. The given statement means something more than this. In addition to what I thought, it also means that if A does not imply B or B does not imply C, then A does not imply C as well. This however doesn't happen. The truth table shows that even though A does not imply B or B does not imply C, it is possible that A implies C.

As far as my intuition is concerned, the following would be the right way to represent what I was thinking.

((A ⇒ B) ∧ (B ⇒ C)) ⇒ (A ⇒ C)

Indeed, this statement is valid as can be seen from the truth table below.

ABC A ⇒ BB ⇒ CA ⇒ C (A ⇒ B) ∧ (B ⇒ C) ((A ⇒ B) ∧ (B ⇒ C)) ⇒ (A ⇒ C)
TTT TTT T T
TTF TFF F T
TFT FTT F T
TFF FTF F T
FTT TTT T T
FTF TFT F T
FFT TTT T T
FFF TTT T T
[mathematics]
3 comments

Random post

Saturday, October 1, 2011

Let me share a very interesting C programming puzzle that I learnt from one of my colleagues.

Consider the following code-snippet written in the C programming language.

int i;
for (i = 0; i < 6; i--)
    printf(".");
This code invokes undefined behaviour. The value in variable i decrements to INT_MIN after |INT_MIN| + 1 iterations. In the next iteration, there is a negative overflow which is undefined for signed integers in C. On many implementations though, INT_MIN − 1 would result in INT_MAX which is not less than 6 and thus the loop would terminate. With such implementations, this code would print |INT_MIN| + 1 dots. With 32-bit integers, that would be 2147483649 dots. However, it could also be optimized into an endless loop by the compiler if it wants to exploit the fact that a negative overflow for signed integers is undefined. gcc in fact does optimize that to an endless loop if you compile it with the -O3 option.

Now, here is the puzzle.

Add or modify exactly one operator in the following code such that it prints exactly 6 dots.

int i;
for (i = 0; i < 6; i--)
    printf(".");

An obvious solution is:

int i;
for (i = 0; i < 6; i++)
    printf(".");

There are at least 3 other solutions. I found one of them very interesting. I'll discuss the interesting solution tomorrow. Till then, feel free to try and post your solutions below as comments.

Update: The discussion on the solution is now available at: Solutions to 'Loopy C Puzzle'.

[programming]
11 comments