<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>

    <title>Susam Pal</title>
    <link>http://susam.in/blog/</link>
    <description>A blog on puzzles, mathematics, science and technology.</description>
    <language>en</language>
    <!-- #implication-of-implications - Implication of implications -->
    <item>
        <title>Implication of implications</title>
        <link>http://susam.in/blog/implication-of-implications/</link>
        <description>
            &lt;p&gt;
Last night, I came across a simple propositional logic problem concerned with
the validity of the following statement:
&lt;/p&gt;
&lt;p class=&quot;math&quot;&gt;((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;hArr; (A &amp;rArr; C)&lt;/p&gt;
&lt;p&gt;
At first, I thought it is valid. But on drawing the truth table, I was
surprised to see that it isn't valid.
&lt;/p&gt;

&lt;table class=&quot;data&quot; border=&quot;1&quot; cellpadding=&quot;5px&quot;&gt;
&lt;tr&gt;
    &lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;C&lt;/th&gt;
    &lt;th&gt;A &amp;rArr; B&lt;/th&gt;&lt;th&gt;B &amp;rArr; C&lt;/th&gt;&lt;th&gt;A &amp;rArr; C&lt;/th&gt;
    &lt;th&gt;(A &amp;rArr; B) &amp;and; (B &amp;rArr; C)&lt;/th&gt;
    &lt;th&gt;((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;hArr; (A &amp;rArr; C)&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;!--
&lt;pre&gt;
 A  B  C    A &amp;rArr; B  B &amp;rArr; C  C &amp;rArr; A    (A &amp;rArr; B) &amp;and; (B &amp;rArr; C)    ((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;hArr; (A &amp;rArr; C)
---------  ---------------------  -------------------  -------------------------------
 T  T  T      T      T      T              T                          T
 T  T  F      T      F      F              F                          T
 T  F  T      F      T      T              F                          F
 T  F  F      F      T      F              F                          T
 F  T  T      T      T      T              T                          T
 F  T  F      T      F      T              F                          F
 F  F  T      T      T      T              T                          T
 F  F  F      T      T      T              T                          T
&lt;/pre&gt;
--&gt;

&lt;p&gt;
My intuition had failed. The truth table shows that there are two cases in
which the statement is false.
&lt;/p&gt;

&lt;h3&gt;Case 1: (A &amp;rArr; C) &amp;and; (B &amp;rArr; C) &amp;and; &amp;not;(A &amp;rArr; B)&lt;/h3&gt;
&lt;p&gt;
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.
&lt;/p&gt;&lt;p&gt;
The following two statements are true for any integer x.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
4 | x &amp;rArr; 2 | x&lt;br&gt;
6 | x &amp;rArr; 2 | x
&lt;/p&gt;&lt;p&gt;
But the following statement is false when x &amp;equiv; 4 (mod 12) or x
&amp;equiv; 8 (mod 12).
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
4 | x &amp;rArr; 6 | x
&lt;/p&gt;&lt;p&gt;
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&amp;nbsp;&amp;in;&amp;nbsp;{4,&amp;nbsp;8,&amp;nbsp;16,&amp;nbsp;20,&amp;nbsp;28,&amp;nbsp;32,&amp;nbsp;&amp;hellip;}.
&lt;/p&gt;

&lt;h3&gt;Case 2: (A &amp;rArr; B) &amp;and; (A &amp;rArr; C) &amp;and; &amp;not;(B &amp;rArr; C)&lt;/h3&gt;
&lt;p&gt;
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.
&lt;/p&gt;&lt;p&gt;
The following two statements are true for any integer x.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
6 | x &amp;rArr; 2 | x&lt;br&gt;
6 | x &amp;rArr; 3 | x
&lt;/p&gt;&lt;p&gt;
But the following statement is false when x is an odd multiple of 3.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
2 | x &amp;rArr; 3 | x
&lt;/p&gt;&lt;p&gt;
The truth table correctly tells us that saying, &quot;If an integer is a
mutliple of 6, then it is a multiple of 2 as well as 3,&quot; is not equivalent
to saying, &quot;If an integer is a multiple of 2, it is a multiple of 3
as well.&quot; That would be nonsense.
&lt;/p&gt;

&lt;h3&gt;Encoding intuition correctly&lt;/h3&gt;
&lt;p&gt;
My intuition incorrectly concluded that the
(A&amp;nbsp;&amp;rArr;&amp;nbsp;B)&amp;nbsp;&amp;and;&amp;nbsp;(B&amp;nbsp;&amp;rArr;&amp;nbsp;C)&amp;nbsp;&amp;hArr;&amp;nbsp;(A&amp;nbsp;&amp;rArr;&amp;nbsp;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.
&lt;/p&gt;
&lt;p&gt;
As far as my intuition is concerned, the following would be the right way to
represent what I was thinking.
&lt;/p&gt;
&lt;p class=&quot;math&quot;&gt;((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;rArr; (A &amp;rArr; C)&lt;/p&gt;
&lt;p&gt;
Indeed, this statement is valid as can be seen from the truth table below.
&lt;/p&gt;

&lt;table class=&quot;data&quot; border=&quot;1&quot; cellpadding=&quot;5px&quot;&gt;
&lt;tr&gt;
    &lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;C&lt;/th&gt;
    &lt;th&gt;A &amp;rArr; B&lt;/th&gt;&lt;th&gt;B &amp;rArr; C&lt;/th&gt;&lt;th&gt;A &amp;rArr; C&lt;/th&gt;
    &lt;th&gt;(A &amp;rArr; B) &amp;and; (B &amp;rArr; C)&lt;/th&gt;
    &lt;th&gt;((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;rArr; (A &amp;rArr; C)&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;&lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
    &lt;td&gt;T&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;!--
&lt;pre&gt;
 A  B  C    A &amp;rArr; B  B &amp;rArr; C  C &amp;rArr; A    (A &amp;rArr; B) &amp;and; (B &amp;rArr; C)    ((A &amp;rArr; B) &amp;and; (B &amp;rArr; C)) &amp;rArr; (A &amp;rArr; C)
---------  ---------------------  -------------------  -------------------------------
 T  T  T      T      T      T              T                          T
 T  T  F      T      F      F              F                          T
 T  F  T      F      T      T              F                          T
 T  F  F      F      T      F              F                          T
 F  T  T      T      T      T              T                          T
 F  T  F      T      F      T              F                          T
 F  F  T      T      T      T              T                          T
 F  F  F      T      T      T              T                          T
&lt;/pre&gt;
--&gt;
        </description>
        <pubDate>Tue, 22 Nov 2011 14:00:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/implication-of-implications/</guid>
    </item>
    <!-- #auth-cram-md5 - AUTH CRAM-MD5 -->
    <item>
        <title>AUTH CRAM-MD5</title>
        <link>http://susam.in/blog/auth-cram-md5/</link>
        <description>
            &lt;p&gt;
I was playing with a few SMTP authentication mechanisms while setting up
my SMTP server. I found CRAM-MD5 authentication mechanism particularly
interesting as it is a challenge-response authentication mechanism and
involves HMAC-MD5. Let me first show a complete session with an SMTP
server before coming to CRAM-MD5. Note that I am not using TLS or SSL in
the sessions displayed below so that the SMTP traffic can be seen as
plaintext.
In practice, any email program should be configured to use
TLS or SSL while having a session with an SMTP server. In the following
session, I have selected the PLAIN authentication mechanism.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
susam@nifty:~$ &lt;kbd&gt;telnet susam.in 25&lt;/kbd&gt;
Trying 106.187.41.241...
Connected to susam.in.
Escape character is '^]'.
220 tesseract.susam.in ESMTP Exim 4.72 Mon, 07 Nov 2011 20:27:56 +0530
&lt;kbd&gt;EHLO nifty.localdomain&lt;/kbd&gt;
250-tesseract.susam.in Hello nifty.localdomain [122.167.80.194]
250-SIZE 52428800
250-PIPELINING
250-AUTH PLAIN LOGIN CRAM-MD5
250-STARTTLS
250 HELP
&lt;kbd&gt;AUTH PLAIN AGFsaWNlAHdvbmRlcmxhbmQ=&lt;/kbd&gt;
235 Authentication succeeded
&lt;kbd&gt;MAIL FROM:&amp;lt;alice@susam.in&amp;gt;&lt;/kbd&gt;
250 OK
&lt;kbd&gt;RCPT TO:&amp;lt;example.recipient@gmail.com&amp;gt;&lt;/kbd&gt;
250 Accepted
&lt;kbd&gt;DATA&lt;/kbd&gt;
354 Enter message, ending with &quot;.&quot; on a line by itself
&lt;kbd&gt;Date: Mon, 07 Nov 2011 20:28:00 +0530
From: Alice &amp;lt;alice@susam.in&amp;gt;
To: Example Recipient &amp;lt;example.recipient@gmail.com&amp;gt;
Subject: Test email 

This is a test email.
.&lt;/kbd&gt;
250 OK id=1RNQef-0004e7-7s
&lt;kbd&gt;QUIT&lt;/kbd&gt;
221 tesseract.susam.in closing connection
Connection closed by foreign host.
&lt;/pre&gt;

&lt;p&gt;
In the AUTH PLAIN line I have sent the base64 encoding of the string
&lt;code&gt;\0alice\0wonderland&lt;/code&gt;. &lt;code&gt;\0&lt;/code&gt; indicates a null
character, &lt;code&gt;alice&lt;/code&gt; is the sender's user name and
&lt;code&gt;wonderland&lt;/code&gt; is the sender's password. If an eavesdropper
intercepts this network traffic, he can easily find the user's password
by simply decoding the base64 response sent by the client. This is also
susceptible to replay attacks as the eavesdropper can use the &lt;code&gt;AUTH
PLAIN&lt;/code&gt; line containing the base64 encoded credentials to log into
the server in future. This is why it is very important to secure the
connection with TLS or SSL while having a session with the SMTP server.
&lt;/p&gt;&lt;p&gt;
Now, let me quickly show only the relevant lines for an authentication
using the LOGIN mechanism.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
&lt;kbd&gt;AUTH LOGIN&lt;/kbd&gt;
334 VXNlcm5hbWU6
&lt;kbd&gt;YWxpY2U=&lt;/kbd&gt;
334 UGFzc3dvcmQ6
&lt;kbd&gt;d29uZGVybGFuZA==&lt;/kbd&gt;
235 Authentication succeeded
&lt;/pre&gt;

&lt;p&gt;
What is happening here becomes clear by decoding the base64 encoded
messages. The following statements in Python 2.7.2 decode these
messages.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'VXNlcm5hbWU6'.decode('base64')&lt;/kbd&gt;
'Username:'
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'YWxpY2U='.decode('base64')&lt;/kbd&gt;
'alice'
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'UGFzc3dvcmQ6'.decode('base64')&lt;/kbd&gt;
'Password:'
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'d29uZGVybGFuZA=='.decode('base64')&lt;/kbd&gt;
'wonderland'
&lt;/pre&gt;

&lt;p&gt;
If the session isn't encrypted, LOGIN authentication mechanism is
susceptible to the same problems that PLAIN authentication mechanism is
susceptible to. Now, let me describe the CRAM-MD5 authentication
mechanism. When the client selects the CRAM-MD5 authentication
mechanism, the server sends a base64 encoded challenge as shown below.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
&lt;kbd&gt;AUTH CRAM-MD5&lt;/kbd&gt;
334 PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg==
&lt;/pre&gt;

&lt;p&gt;
An HMAC is calculated for this challenge with the password as the key
and MD5 as the hash function. A string is formed by concatenating the
user name, a space and the hex representation of the HMAC. The base64
encoding of this string is sent as the response by the client. The
following statements I tried in Python 2.7.2 show how a response can be
formed for the above challenge.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg=='.decode('base64')&lt;/kbd&gt;
'&amp;lt;17893.1320679123@tesseract.susam.in&amp;gt;'
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;import hmac, hashlib&lt;/kbd&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;hmac.new('wonderland', '&amp;lt;17893.1320679123@tesseract.susam.in&amp;gt;', hashlib.md5).hexdigest()&lt;/kbd&gt;
'64b2a43c1f6ed6806a980914e23e75f0'
&amp;gt;&amp;gt;&amp;gt; &lt;kbd&gt;'alice 64b2a43c1f6ed6806a980914e23e75f0'.encode('base64')&lt;/kbd&gt;
'YWxpY2UgNjRiMmE0M2MxZjZlZDY4MDZhOTgwOTE0ZTIzZTc1ZjA=\n'
&lt;/pre&gt;

&lt;p&gt;
Of course, this can be written as a small function:
&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;
import hmac, hashlib
def cram_md5_response(username, password, base64challenge):
    return (username + ' ' +
            hmac.new(password,
                     base64challenge.decode('base64'),
                     hashlib.md5).hexdigest()).encode('base64')
&lt;/pre&gt;

&lt;p&gt;
The following snippet shows the SMTP server accepting the
client-response.
&lt;/p&gt;

&lt;pre class=&quot;terminal&quot;&gt;
&lt;kbd&gt;AUTH CRAM-MD5&lt;/kbd&gt;
334 PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg==
&lt;kbd&gt;YWxpY2UgNjRiMmE0M2MxZjZlZDY4MDZhOTgwOTE0ZTIzZTc1ZjA=&lt;/kbd&gt;
235 Authentication succeeded
&lt;/pre&gt;

&lt;p&gt;
CRAM-MD5 authentication mechanism is relatively more secure than the
other two mechanisms even if the connection is not secured because the
password can not be retrieved by decoding the base64 encoded
client-response. The password is used as the key to calculate the HMAC
but the password is not present anywhere in the response. It prevents
replay attacks too because the server sends an unpredictable challenge
for every authentication. The client-response computed for a certain
challenge is invalid for further authentications which will involve
different unpredictable challenges.
&lt;/p&gt;

&lt;p&gt;
Here is a list of hyperlinks for further reading:
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://tools.ietf.org/html/rfc4954&quot;&gt;RFC 4954&lt;/a&gt;:
    SMTP Service Extension for Authentication
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tools.ietf.org/html/rfc4616&quot;&gt;RFC 4616&lt;/a&gt;:
    The PLAIN Simple Authentication and Security Layer (SASL) Mechanism
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tools.ietf.org/html/rfc2195&quot;&gt;RFC 2195&lt;/a&gt;:
    IMAP/POP AUTHorize Extension for Simple Challenge/Response
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tools.ietf.org/html/rfc2104&quot;&gt;RFC 2104&lt;/a&gt;:
    HMAC: Keyed-Hashing for Message Authentication&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;        </description>
        <pubDate>Mon, 07 Nov 2011 20:27:56 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/auth-cram-md5/</guid>
    </item>
    <!-- #when-worse-is-better - When worse is better -->
    <item>
        <title>When worse is better</title>
        <link>http://susam.in/blog/when-worse-is-better/</link>
        <description>
            &lt;p&gt;
Let me begin this post with a puzzle.
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
&lt;p&gt;
Alex and Bob worked as financial advisers for the same company. They
drew equal salaries from the company. They behaved well at the office.
Both worked on similar assignments. Each assignment required a yes-no
decision. The company used the decisions made by them to make profits.
&lt;/p&gt;
&lt;p&gt;
After the recession hit the company very badly, they have to fire one of
them. Both Alex and Bob worked on almost the same number of assignments
in the last ten years. Alex has been consistently taking about 80%
decisions correctly every year. Bob, on the other hand, has been taking
only about 5% correct decisions every year.
&lt;/p&gt;&lt;p&gt;
The company has decided to keep Bob and fire Alex. Why?
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
If you want to solve this puzzle yourself, you might want to stop here
and think for a while. There are spoilers ahead.
&lt;/p&gt;&lt;p&gt;
Before giving away the solution to this puzzle, let me discuss
something else.
&lt;/p&gt;
Shannon's noisy channel coding theorem guarantees that in a binary
symmetric channel with an error rate p, it is possible to transmit
digital data with arbitrarily small probability of error up to a
transmission rate of:
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
1 + p log&lt;sub&gt;2&lt;/sub&gt; p + (1 &amp;minus; p) log&lt;sub&gt;2&lt;/sub&gt; (1 &amp;minus; p)
&lt;/p&gt;&lt;p&gt;
We'll call this the optimal transmission rate. The error rate p is the
probability that the channel will corrupt a bit from 1 to 0 or vice
versa. The transmission rate is the ratio of the number of data bits
to the total number of bits used to represent them. If we employ
error-correcting codes, the transmission rate would be less than 1
because we'll need extra bits for error correction. As a result, the
number of bits used to represent them would be more than the number
of data bits.
&lt;/p&gt;
&lt;div class=&quot;center&quot;&gt;
&lt;img src=&quot;/files/blog/when-worse-is-better/transmission-rate.png&quot;
     alt=&quot;graph&quot;&gt;
&lt;/div&gt;
&lt;p&gt;
For p&amp;nbsp;=&amp;nbsp;0, the transmission rate is maximum. That's obvious.
Loosely speaking, if the channel is error-free, we do not need extra bits
for error correction. The transmission rate is poorest when
p&amp;nbsp;=&amp;nbsp;&lt;sup&gt;1&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt;. In this case, each bit
received is completely random since the channel is just as likely to
send 0 or 1 irrespective of the original bit sent by the transmitter.
However, it might be surprising that as the error rate increases from
0.5 to 1, the optimal transmission rate increases. When p&amp;nbsp;=&amp;nbsp;1,
the optimal transmission rate is maximum again.  It is easy to undersand
this intuitively. At p&amp;nbsp;=&amp;nbsp;1, the channel is guaranteed to
corrupt each bit. So, the receiver can correct the error by inverting
each bit in the received message. So, we do not need extra bits for
error correction in this case as well.
&lt;/p&gt;&lt;p&gt;
Now, let me get back to the puzzle I mentioned while beginning this
post.
&lt;/p&gt;&lt;p&gt;
The company decided to keep Bob because he was more useful to the
company. He helped the company to take 95% correct decisions. They
simply did the opposite of what Bob recommended and made huge profits in
the last ten years. Alex on the other hand helped them take only 80%
decisions correctly. So, he has to go. It's unfair but it's
mathematically correct.
&lt;/p&gt;        </description>
        <pubDate>Sat, 29 Oct 2011 02:00:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/when-worse-is-better/</guid>
    </item>
    <!-- #re-infosys-tcs-or-wipro - Re: Infosys, TCS, or Wipro? -->
    <item>
        <title>Re: Infosys, TCS, or Wipro?</title>
        <link>http://susam.in/blog/re-infosys-tcs-or-wipro/</link>
        <description>
            &lt;p&gt;
The &lt;a href=&quot;/blog/infosys-tcs-or-wipro/&quot;&gt;'Infosys, TCS, or Wipro?'&lt;/a&gt;
post is doing the rounds again and some hatred as well as love is
arriving on my email. That's fine. I've got pretty used to it in the
last 5 months. If you try to say the truth, some people are going to
call you arrogant and offensive. I would take hatred any day rather than
needlessly sugarcoat things to please everybody.
&lt;/p&gt;&lt;p&gt;
So, I can understand when someone dislikes the way I have expressed the
whole matter in that blog post. But I fail to understand the motivation
behind any employee of these organizations trying to justify that
working there is almost as good as working for a company where you can
work with a team of very talented engineers. There have been &lt;a
href=&quot;http://hackerstreet.in/item?id=6323&quot;&gt;plenty&lt;/a&gt;&amp;nbsp;&lt;a
href=&quot;http://news.ycombinator.com/item?id=2628318&quot;&gt;of&lt;/a&gt;&amp;nbsp;&lt;a
href=&quot;http://www.reddit.com/r/india/comments/lqemo/why_a_fresher_should_not_join_infosys_tcs_or_wipro/&quot;&gt;arguments&lt;/a&gt;
on the web. I'll discuss some of the points made in the arguments.
&lt;/p&gt;
&lt;ol class=&quot;contentlist&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#india&quot;&gt;India needs IT services companies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#business-vs-engineering&quot;&gt;Business problems vs. engineering problems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#business-and-engineering&quot;&gt;Business problems and
engineering problems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#engineering&quot;&gt;Engineering in Adobe, Amazon, Google, etc.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#passion&quot;&gt;Passionate people can do engineering anywhere&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href=&quot;#cannot&quot;&gt;But everyone can not join Adobe, Amazon, Google, etc.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#engineers&quot;&gt;Infosys, TCS and Wipro have good engineers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#training&quot;&gt;Training&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#meritocracy&quot;&gt;Meritocracy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#leaving&quot;&gt;Leaving Infosys, TCS, or Wipro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#algorithms&quot;&gt;Solving algorithm problems vs. editing configuration&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ol class=&quot;paralist&quot;&gt;
&lt;li id=&quot;india&quot;&gt;&lt;b&gt;India needs IT services companies:&lt;/b&gt;
The number one argument has been that India needs IT services companies
because they make a lot of money and they are very successful. I don't
understand why anyone would attack my post with this point when I have
never said anything to the contrary. I agree with this point. However,
in my opinion, students who have learnt their engineering well during
the course, and now want to improve their engineering skills during work
shouldn't work for one of these companies. They should join &lt;a
href=&quot;/blog/infosys-tcs-or-wipro/#suggestion&quot;&gt;a start-up or a company
known for hiring skilled engineers&lt;/a&gt;.
&lt;/li&gt;
&lt;li id=&quot;business-vs-engineering&quot;&gt;&lt;b&gt;Business problems vs. engineering
problems:&lt;/b&gt; The second most popular argument has been that a software
company is not a place to showcase your engineering talent. They argue
that it is a place to solve business problems, not engineering problems.
I disagree with this very firmly. Business problems and engineering
problems are not mutually exclusive. The reason why many employees of
these companies feel that solving a business problem wouldn't involve an
engineering problem is that they are not technically sound enough to
even realize whether a particular problem needs to be solved with some
concept they learnt in their engineering course. I've said this before
and I'll say it again. Engineering problems are there in every software
company. It needs the mind of an engineer to identify those problems and
solve it correctly. Not all string parsing problems should be solved
with a chain of string library functions stitched together. Some string
processing and parsing work can be done more efficiently and reliably by
coding a simple &lt;a
href=&quot;http://en.wikipedia.org/wiki/Deterministic_finite-state_machine&quot;&gt;DFA&lt;/a&gt;.
Not all data should be stored as records in an array. Some need to be
stored as &lt;a href=&quot;http://mathworld.wolfram.com/Graph.html&quot;&gt;graphs&lt;/a&gt;.
Knowing regular expressions can be dangerous if one doesn't know what
kind of patterns they can not express.
&lt;/li&gt;
&lt;li id=&quot;business-and-engineering&quot;&gt;&lt;b&gt;Business problems &amp;amp; engineering
problems:&lt;/b&gt; If you want to solve business problems and keep your
customers happy, Infosys, TCS, or Wipro might be the right place for
you. If you want to solve business problems, keep your customers happy
and learn a great deal of engineering while doing all these, you need to
find another workplace where you can work with a team of skilled
engineers who you can give you constructive feedback or criticism on
your work.
&lt;/li&gt;
&lt;li id=&quot;engineering&quot;&gt;&lt;b&gt;Engineering in Adobe, Amazon, Google, etc.:&lt;/b&gt;
A popular myth that prevails among many is that there is no engineering
to do in the Indian offices of Adobe, Amazon, Google, etc. 
I'll reiterate that there is engineering to do in every
software company, even in Infosys, TCS and Wipro. It really depends on
the people whether they want to do engineering sincerely or not.  Some
people would somehow develop the required software by stitching together
libraries and give up or look for less-efficient workarounds when a
particular task requires inventing a new engineering solution. There is
another kind of people who are ready to invent new engineering solutions
with all the engineering knowledge they have acquired during college and
professional life. They do not hesitate to re-invent the wheel when the
existing libraries do not meet the requirements of the particular
problem at hand. Such engineers are more in number in start-ups like
Gluster, Parallocity, SlideShare, etc. and companies like Adobe,
Amazon, Google, etc. These companies also collaborate with universties
to learn new techniques and concepts to solve business problems. These
companies do not look for less-efficient workarounds when faced with a
brick wall. They challenge themselves and do everything possible to
solve a problem in the best possible way. These are the places where
freshers can hope to learn a great deal of engineering from people around
them.
&lt;/li&gt;
&lt;li id=&quot;passion&quot;&gt;&lt;b&gt;Passionate people can do engineering anywhere:&lt;/b&gt;
Another popular argument is that it doesn't matter where you work. One
can do engineering anywhere, be it Google or Infosys. I agree with this.
But then, given a place like Adobe, Amazon, or Google where you can get
very good feedback on your work from talented colleagues around you and
another place like Infosys, TCS, or Wipro where clueless people around
you would call you great because you solved a problem and hail you as a
genius, where do you think you are going to learn more and improve
yourself faster?
&lt;/li&gt;
&lt;li id=&quot;cannot&quot;&gt;&lt;b&gt;But everyone can not join Adobe, Amazon, Google, etc.:&lt;/b&gt;
Well, I never said that everyone should get into a reputed software product
company. Not everyone wants to make a career in engineering. Some people
have other goals in life. They just want a regular job. It sounds
perfectly fine to me. My post was written for those who aspire to make a
career in engineering. In my opinion, they should try to get into a
reputed software product company or a start-up that is known to hire
skilled engineers. If they aren't well prepared, the only thing one can
do is to prepare well to fulfill their aspirations. Some people say that
they should join Infosys, TCS and Wipro to earn a living while they are
preparing and I think it's a great advice. I'll suggest considering
contributing some new features or bug-fixes they would like to see in
some open source project. &lt;a href=&quot;http://incubator.apache.org/&quot;&gt;Apache
Incubator&lt;/a&gt; has a lot of budding projects. They can try solving
problems at &lt;a href=&quot;http://community.topcoder.com/tc&quot;&gt;TopCoder&lt;/a&gt;, &lt;a
href=&quot;http://www.codechef.com/&quot;&gt;CodeChef&lt;/a&gt;, &lt;a
href=&quot;http://www.spoj.pl/&quot;&gt;SPOJ&lt;/a&gt;, &lt;a
href=&quot;http://projecteuler.net/&quot;&gt;Project Euler&lt;/a&gt;, etc. All these things
are good ways of preparing in my opinion.
&lt;/li&gt;
&lt;li id=&quot;engineers&quot;&gt;&lt;b&gt;Infosys, TCS and Wipro have good engineers:&lt;/b&gt;
Many people have told me that there are good engineers in Infosys, TCS
and Wipro as well. I agree. I never mentioned that 100% of the engineers
in these companies don't do engineering. I mentioned that the majority
of them don't do it. In fact, drawing from my personal experience, I
quoted a figure that only 1 out of 200 are capable of engineering in
these companies. Like most guess work, this figure could be wrong but I
am pretty confident that the bigger picture I am trying to paint here
isn't.
&lt;/li&gt;
&lt;li id=&quot;training&quot;&gt;&lt;b&gt;Training:&lt;/b&gt;
Many claimed that I am wrong about the poor standard of training in
Infosys, TCS, or Wipro. I must tell them that I have attended some of
these training programmes. Among the many horror stories pertaining to
training in these companies, I'll share only one with you to make my
point. In the training assessments, the instructors set question papers
containing problems with &lt;a href=&quot;/blog/sequence-points/&quot;&gt;code that
invokes undefined behaviour&lt;/a&gt; and ask you to predict its output. 'It
invokes undefined behaviour' is not provided as an option you can select
as the correct answer. Such training and knowledge is not only inaccurate
but also very dangerous if you care about robustness and security of the
software you create.
&lt;/li&gt;
&lt;li id=&quot;meritocracy&quot;&gt;&lt;b&gt;Meritocracy:&lt;/b&gt;
A few people felt that I should have mentioned that these companies
do not practice meritocracy. Anyone with sufficient number of years of
experience can get a promotion in these companies. This is true. It
didn't occur to me while writing that post. I was focussed on the
popular myths about these companies that prevail among college students
while writing the post.
&lt;/li&gt;
&lt;li id=&quot;leaving&quot;&gt;&lt;b&gt;Leaving Infosys, TCS, or Wipro:&lt;/b&gt;
Some people commented that people like me just leave these companies and
discourage others from joining them. They feel that we choose the easy
way out. They would rather have us stay in these companies and improve
them. Firstly, getting a job in a reputed company where you can get an
opportunity to work on interesting engineering problems is not easy. You
have to be passionate about your area of interest. You have to work and
prepare yourself for it. Secondly, Infosys, TCS and Wipro were made by
their founders with a particular vision of providing IT services. They
have achieved what they wanted with great success. The improvements that
you want to make in these companies may not be consistent with the
vision of the founders.
&lt;/li&gt;
&lt;li id=&quot;algorithms&quot;&gt;&lt;b&gt;Solving algorithm problems vs. editing configuration:&lt;/b&gt;
This has to be the strangest argument I have come across. Someone
mentioned that a person who solves algorithm problems earns no more than
someone who only edits configuration files. After seeing both sides of
the software industry, I find this false. Solving an algorithm problem
is one thing.  Solving an algorithm problem efficiently is an entirely
different thing.  I have seen people who can solve difficult algorithm
problems efficiently earning at least twice that of an Infosys engineer.
I have numerous examples for this: a friend who is solving problems to
deliver the best news from the entire web to the users and another who
writes algorithms for a cluster of Nvidia GPUs to do real-time analysis
of network traffic to keep attackers out of the network. I have seen
people solving algorithm problems in Topcoder, Codechef, etc.
earning very well as software developers. One might argue that those
toy problems tell you nothing about one's professional programming
ability. But one has to agree that it at least tells you that someone
who can solve difficult and contrived problems set by some of the most
intelligent brains on the planet must be good at solving real software
engineering problems which for the most part are less challenging
algorithm-wise and more challenging design-wise. The toppers from these
contests earn way more than someone who only edits configuration files.
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
To summarize, that post was meant for and only for people who have what
it takes to work at a place where he can solve engineering problems. I
have known many such people who were good engineers but weren't aware of
the reality in companies like Infosys, TCS, or Wipro before joining
them. That post was meant for these people. I devoted a couple of
paragraphs in the previous post to make this clear but for some reason
most people seem to have ignored them.
&lt;/p&gt;        </description>
        <pubDate>Fri, 28 Oct 2011 03:07:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/re-infosys-tcs-or-wipro/</guid>
    </item>
    <!-- #solutions-to-loopy-c-puzzle - Solutions to 'Loopy C puzzle' -->
    <item>
        <title>Solutions to 'Loopy C puzzle'</title>
        <link>http://susam.in/blog/solutions-to-loopy-c-puzzle/</link>
        <description>
            &lt;p&gt;
In &lt;a href=&quot;/blog/loopy-c-puzzle/&quot;&gt;my blog post yesterday&lt;/a&gt;, I shared
this puzzle:
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
&lt;p&gt;
Add or modify exactly one operator in the following code such that it
prints exactly 6 dots.
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
int i;
for (i = 0; i &amp;lt; 6; i--)
    printf(&quot;.&quot;);
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Let me first list all the possible solutions I know.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;for (i = 0; i &amp;lt; 6; i&lt;span style=&quot;color: red&quot;&gt;++&lt;/span&gt;)&lt;/code&gt;
&lt;/li&gt;&lt;li&gt;
&lt;code&gt;for (i = 0; i &amp;lt; 6; &lt;span style=&quot;color: red&quot;&gt;++&lt;/span&gt;i)&lt;/code&gt;
&lt;/li&gt;&lt;li&gt;
&lt;code&gt;for (i = 0; &lt;span style=&quot;color: red&quot;&gt;-&lt;/span&gt;i &amp;lt; 6; i--)&lt;/code&gt;
&lt;/li&gt;&lt;li&gt;
&lt;code&gt;for (i = 0; i &lt;span style=&quot;color: red&quot;&gt;+&lt;/span&gt; 6; i--)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;for (i = 0; i &lt;span style=&quot;color: red&quot;&gt;^=&lt;/span&gt; 6; i--)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The last solution in the list is not quite obvious and needs a little
analysis to understand why it works.
&lt;/p&gt;&lt;p&gt;
To generalize the problem, let us use n instead of 6. So, the loop
becomes:
&lt;pre class=&quot;code&quot;&gt;
int i;
for (i = 0; i ^= n; i--)
    printf(&quot;.&quot;);
&lt;/pre&gt;
If we use XOR to represent bitwise exclusive OR, first i is set to i = n
XOR i before we enter the loop. Since, the initial value of i is 0, this
amounts to setting i = n before we enter the loop.
&lt;/p&gt;&lt;p&gt;
Thereafter, every iteration prints a dot and sets
i&amp;nbsp;=&amp;nbsp;n&amp;nbsp;XOR&amp;nbsp;(i&amp;nbsp;-&amp;nbsp;1). If we try to represent
two such consecutive iterations as one operation, this composite
operation prints two dots and sets
i&amp;nbsp;=&amp;nbsp;n&amp;nbsp;XOR&amp;nbsp;{[n&amp;nbsp;XOR&amp;nbsp;(i&amp;nbsp;-&amp;nbsp;1)]&amp;nbsp;-&amp;nbsp;1}
where i on the RHS is the value of i before the pair of iterations and i
on the LHS is the value of i after the pair of iterations complete.
&lt;b&gt;&amp;hellip; (A)&lt;/b&gt;
&lt;/p&gt;&lt;p&gt;
Before we proceed further, note that
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
k XOR 1 = k - 1 for an odd integer k. &lt;b&gt;&amp;hellip; (1)&lt;/b&gt;
&lt;/li&gt;&lt;li&gt;
k XOR 1 = k + 1 for an even integer k. &lt;b&gt;&amp;hellip; (2)&lt;/b&gt;
&lt;/li&gt;&lt;li&gt;
a XOR b is an odd integer if a and b have different parities. &lt;b&gt;&amp;hellip; (3)&lt;/b&gt;
&lt;/li&gt;&lt;/ul&gt;
&lt;/ul&gt;
&lt;p&gt;
I'm not presenting the proof of these results since they are very
trivial. We'll use these results in the discussion below.
&lt;/p&gt;&lt;p&gt;
If n is odd, i is odd initially because i is set to n before we enter
the loop. We'll show that i always remains odd and never changes for
every pair of iterations.
&lt;p class=&quot;math&quot;&gt;
i = n XOR {[n XOR (i - 1)] - 1} &amp;nbsp;&amp;nbsp; &amp;hellip; from (A)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= n XOR {[n XOR (i - 1)] XOR 1} &amp;nbsp;&amp;nbsp; &amp;hellip; from
(1) and (3)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= (n XOR n) XOR [(i - 1) XOR 1] &amp;nbsp;&amp;nbsp; &amp;hellip; from
associativity of XOR&lt;br&gt;
&amp;nbsp;&amp;nbsp;= 0 XOR [(i - 1) + 1] &amp;nbsp;&amp;nbsp; &amp;hellip; from (2) and
(3)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= i
&lt;/p&gt;&lt;p&gt;
Note that i begins as i = n. Since, i is set to n again
after every two iterations, the loop never terminates.
&lt;/p&gt;&lt;p&gt;
If n is even, i is even initially because i is set to n before we enter
the loop. We'll show that i always remains even and decrements by 2 for
every pair of iterations.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
i = n XOR {[n XOR (i - 1)] - 1} &amp;nbsp;&amp;nbsp; &amp;hellip; from (A)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= n XOR {[n XOR (i - 1)] XOR 1} &amp;nbsp;&amp;nbsp; &amp;hellip; from
(1) and (3)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= (n XOR n) XOR [(i - 1) XOR 1] &amp;nbsp;&amp;nbsp; &amp;hellip; from
associativity of XOR&lt;br&gt;
&amp;nbsp;&amp;nbsp;= 0 XOR [(i - 1) - 1] &amp;nbsp;&amp;nbsp; &amp;hellip; from (1) and
(3)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= i - 2
&lt;/p&gt;&lt;p&gt;
Since i begins as i = n and decrements by 2 for every two
iterations, the loop terminates after n iterations thereby printing n
dots.
&lt;/p&gt;        </description>
        <pubDate>Sat, 01 Oct 2011 22:46:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/solutions-to-loopy-c-puzzle/</guid>
    </item>
    <!-- #loopy-c-puzzle - Loopy C puzzle -->
    <item>
        <title>Loopy C puzzle</title>
        <link>http://susam.in/blog/loopy-c-puzzle/</link>
        <description>
            &lt;p&gt;
Let me share a very interesting C programming puzzle that I learnt from
one of my colleagues.
&lt;/p&gt;&lt;p&gt;
Consider the following code-snippet written in the C programming
language.
&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;
int i;
for (i = 0; i &amp;lt; 6; i--)
    printf(&quot;.&quot;);
&lt;/pre&gt;
This code invokes undefined behaviour. The value in variable
&lt;code&gt;i&lt;/code&gt; decrements to &lt;a
href=&quot;http://tigcc.ticalc.org/doc/limits.html&quot;&gt;INT_MIN&lt;/a&gt; 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 &amp;minus; 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
&lt;code&gt;-O3&lt;/code&gt; option.
&lt;/p&gt;&lt;p&gt;
Now, here is the puzzle.
&lt;/p&gt;

&lt;div class=&quot;problem&quot;&gt;
&lt;p&gt;
Add or modify exactly one operator in the following code such that it
prints exactly 6 dots.
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
int i;
for (i = 0; i &amp;lt; 6; i--)
    printf(&quot;.&quot;);
&lt;/pre&gt;
&lt;/div&gt;

&lt;/p&gt;&lt;p&gt;
An obvious solution is:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
int i;
for (i = 0; i &amp;lt; 6; i&lt;b style=&quot;color: red&quot;&gt;++&lt;/b&gt;)
    printf(&quot;.&quot;);
&lt;/pre&gt;
&lt;p&gt;
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.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; The discussion on the solution is now available at: &lt;a
href=&quot;/blog/solutions-to-loopy-c-puzzle/&quot;&gt;Solutions to 'Loopy C Puzzle'&lt;/a&gt;.
&lt;/p&gt;        </description>
        <pubDate>Sat, 01 Oct 2011 01:45:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/loopy-c-puzzle/</guid>
    </item>
    <!-- #from-tower-of-hanoi-to-counting-bits - From the Tower of Hanoi to counting bits -->
    <item>
        <title>From the Tower of Hanoi to counting bits</title>
        <link>http://susam.in/blog/from-tower-of-hanoi-to-counting-bits/</link>
        <description>
            &lt;p&gt;

&lt;a href=&quot;/files/blog/tower-of-hanoi/rise_of_the_planet_of_the_apes.jpg&quot;&gt;
&lt;img width=&quot;216&quot;
     src=&quot;/files/blog/tower-of-hanoi/rise_of_the_planet_of_the_apes.jpg&quot;
     alt=&quot;Rise of the Planet of the Apes (Poster)&quot;
     style=&quot;float: right; margin-left: 10px&quot;&gt;&lt;/a&gt;

A few weeks ago, I watched &lt;a
href=&quot;http://www.imdb.com/title/tt1318514/&quot;&gt;Rise of the Planet of the
Apes&lt;/a&gt; in a movie theatre. The movie showed a genetically engineered
chimpanzee trying to solve a puzzle involving 4 discs, initially stacked
in decreasing size on one of three pegs. The chimpanzee was supposed to
transer the entire stack to one of the other pegs, moving only one disc
at a time and never moving a larger one onto a smaller one.
&lt;/p&gt;&lt;p&gt;
The problem was called &lt;em&gt;the Lucas' problem&lt;/em&gt; in the movie. I
couldn't help myself from remarking to my friend sitting next to me that
the problem is actually known as &lt;em&gt;the Tower of Hanoi&lt;/em&gt; and the
minimum number of moves required to solve the problem is
2&lt;sup&gt;n&lt;/sup&gt;&amp;nbsp;&amp;minus;&amp;nbsp;1&lt;/sup&gt; if there are n discs involved.
The math lectures were not very welcome by her in the middle of the
movie.
&lt;/p&gt;&lt;p&gt;
During the intermission, she was curious to know why it was called the
Lucas' problem instead of the Tower of Hanoi in the movie. I think it's
probably because the puzzle was invented by the French mathematician
Édouard Lucas in 1883. There is another version of this problem known
as &lt;em&gt;the Tower of Brahma&lt;/em&gt; that involves 64 discs made of pure gold
and three diamond needles. According to a legend, a group of Brahmin
priests are working at the problem and the world will end when the last
move of the puzzle is completed. Now, even if they make one move every
second, it'll take 18,446,744,073,709,551,615 seconds to complete the
puzzle. That's about 584.54 billion years.
&lt;/p&gt;&lt;p&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Tower_of_Hanoi&quot;&gt;
&lt;img src=&quot;/files/blog/tower-of-hanoi/solution.gif&quot;
     alt=&quot;Animated solution&quot;
     style=&quot;float: left; margin-right: 10px&quot;&gt;&lt;/a&gt;
Since, the puzzle in the movie involved only 4 discs, it can be solved
in 2&lt;sup&gt;4&lt;/sup&gt;&amp;nbsp;&amp;minus;&amp;nbsp;1 = 15 moves as illustrated in an
animated solution by André Karwath that I found in the &lt;a
href=&quot;http://en.wikipedia.org/wiki/Tower_of_Hanoi&quot;&gt;Wikipedia article&lt;/a&gt;
for the puzzle.
&lt;/p&gt;&lt;p&gt;
I'll not discuss the solution of this puzzle in this blog post. There
are plenty of articles on the web including the Wikpedia article that
describes why it takes a minimum of 2&lt;sup&gt;n&lt;/sup&gt;&amp;nbsp;&amp;minus;&amp;nbsp;1
moves to complete the puzzle. In this post, I'll talk about an
interesting result I discovered while playing with this puzzle one April
afternoon this year.
&lt;/p&gt;&lt;p&gt;
An interesting thing to note is that if we use T&lt;sub&gt;n&lt;/sub&gt; to denote
the minimum number of moves required to solve the Tower of Hanoi problem
involving n discs, then T&lt;sub&gt;n&lt;/sub&gt; when expressed in binary is the
largest possible n-bit integer. For example,
T&lt;sub&gt;4&lt;/sub&gt;&amp;nbsp;=&amp;nbsp;15&lt;sub&gt;10&lt;/sub&gt;&amp;nbsp;=&amp;nbsp;1111&lt;sub&gt;2&lt;/sub&gt;.
After all, the largest n-bit integer is
2&lt;sup&gt;n&lt;/sup&gt;&amp;nbsp;&amp;minus;&amp;nbsp;1. Now, let me describe the result I
came across while playing with it by asking a question.
&lt;div class=&quot;problem&quot;&gt;
&lt;p&gt;
This problem deals with arbitrary-precision integers. Some definitions:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A positive integer N is called an n-bit integer if and only if
the minimum number of bits required to express the integer is n, or in
other words,
&amp;lfloor;log&lt;sub&gt;2&lt;/sub&gt;(N)&amp;rfloor;&amp;nbsp;+&amp;nbsp;1&amp;nbsp;=&amp;nbsp;n.&lt;/li&gt;
&lt;li&gt;T&lt;sub&gt;n&lt;/sub&gt; represents the largest possible n-bit integer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Some assumptions:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Let the time complexity of adding or subtracting two integers be
O(1).&lt;/li&gt;
&lt;li&gt;Let the time complexity of counting the number of 1-bits in an n-bit
integer be O(n).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
A positive integer n is given.  What is the most efficient way, in terms
of time complexity, to compute the number of 1-bits in T&lt;sub&gt;1&lt;/sub&gt; +
T&lt;sub&gt;2&lt;/sub&gt; + &amp;hellip; + T&lt;sub&gt;n&lt;/sub&gt;?
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The naive approach would involve adding all the n integers and counting
the number of 1-bits in the sum. The time required to add the n integers
is O(n). The sum would require (n&amp;nbsp;+&amp;nbsp;1) bits to be represented.
Hence, it would take O(n) time to count the number of bits in the sum.
Since the sum is (n&amp;nbsp;+&amp;nbsp;1) bits long, the naive approach
requires O(n) memory as well. If n is as large as 2&lt;sup&gt;64&lt;/sup&gt;, over 2
exbibytes of memory would be required to store the sum.
&lt;/p&gt;&lt;p&gt;
There is a more efficient solution.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
T&lt;sub&gt;1&lt;/sub&gt; + &amp;hellip; + T&lt;sub&gt;n&lt;/sub&gt;&lt;br&gt;
&amp;nbsp;= 2&lt;sup&gt;(n + 1)&lt;/sup&gt; &amp;minus; 2 &amp;minus; n&lt;br&gt;
&amp;nbsp;= [2&lt;sup&gt;(n + 1)&lt;/sup&gt; &amp;minus; 1] &amp;minus; (n + 1)
&lt;/p&gt;&lt;p&gt;
Now, 2&lt;sup&gt;(n + 1)&lt;/sup&gt; &amp;minus; 1 has (n + 1) 1-bits.
Subtracting (n&amp;nbsp;+&amp;nbsp;1) from it is equivalent to removing one
1-bit in 2&lt;sup&gt;(n&amp;nbsp;+&amp;nbsp;1)&lt;/sup&gt; &amp;minus; 1 for each 1-bit in
(n&amp;nbsp;+&amp;nbsp;1). So,
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
[number of 1-bits in T&lt;sub&gt;1&lt;/sub&gt; + &amp;hellip; +
T&lt;sub&gt;n&lt;/sub&gt;]&amp;nbsp;&amp;nbsp;=&amp;nbsp;&amp;nbsp;[(n&amp;nbsp;+&amp;nbsp;1)]&amp;nbsp;&amp;nbsp;&amp;minus;&amp;nbsp;&amp;nbsp;[number
of 1-bits in (n&amp;nbsp;+&amp;nbsp;1)].
&lt;/p&gt;&lt;p&gt;
So, the computation involves counting the number of 1-bits in
n&amp;nbsp;+&amp;nbsp;1 which takes O(log&amp;nbsp;n) time and subtracting it from
n&amp;nbsp;+&amp;nbsp;1 which takes O(1) time. The problem is solved in
O(log&amp;nbsp;n) time. It occupies O(log&amp;nbsp;n) memory. What required over
2 exbibytes of memory with the naive approach requires only a little
over 8 bytes of memory with this approach.
&lt;/p&gt;        </description>
        <pubDate>Sun, 25 Sep 2011 12:33:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/from-tower-of-hanoi-to-counting-bits/</guid>
    </item>
    <!-- #langford-pairing - Langford pairing -->
    <item>
        <title>Langford pairing</title>
        <link>http://susam.in/blog/langford-pairing/</link>
        <description>
            &lt;p&gt;
I came across this problem more than 2 years ago when Pranave, a
colleague at &lt;a href=&quot;http://www.rsa.com/&quot;&gt;RSA&lt;/a&gt;, posted this in the
internal mathematics forum.
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
Given a list of first n natural numbers with each natural number
occurring exactly twice in the list, we need to arrange the list in a
manner such that there are exactly k numbers between two k's for each k
in the sequence.
&lt;/div&gt;
&lt;p&gt;
For a small list, say, (1, 1, 2, 2, 3, 3, 4, 4), the answer can be
obtained by trial and error: (4, 1, 3, 1, 2, 4, 3, 2).
&lt;/p&gt;&lt;p&gt;
But what if the list is very long? There is another important question:
Is there a solution for every possible such list? The answer turns out
to be: No. One can easily see that there is no valid arrangements for
(1, 1) or (1, 1, 2, 2) that satisfy the condition specified in the
problem statement.
&lt;/p&gt;&lt;p&gt;
So, we have two things to solve now.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For what values of n is the problem solvable?&lt;/li&gt;
&lt;li&gt;If the problem is solvable for some value of n, how do we solve it?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
In this blog post, I'll show how I solved it. I took some help from the
computer by writing a &lt;a
href=&quot;http://susam.in/downloads/files/langford-pairing/langford.py&quot;&gt;Python
program&lt;/a&gt; to see if I could notice any patterns. Here is the output.
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
n =  1 :          0 solutions (     0 s)
n =  2 :          0 solutions (     0 s)
n =  3 :          2 solutions (     0 s)
n =  4 :          2 solutions (     0 s)
n =  5 :          0 solutions (     0 s)
n =  6 :          0 solutions (     0 s)
n =  7 :         52 solutions (     0 s)
n =  8 :        300 solutions (     0 s)
n =  9 :          0 solutions (     1 s)
n = 10 :          0 solutions (     6 s)
n = 11 :      35584 solutions (    45 s)
n = 12 :     216288 solutions (   390 s)
&lt;/pre&gt;
&lt;p&gt;
From the output, I could form a conjecture:
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
There exists a permutation of (1, 1, 2, 2, &amp;hellip;, n, n) such that for
each k in the sequence (1 &amp;le; k &amp;le; n) there are k numbers between the
two k's if and only if n &amp;equiv; 3 (mod 4) or n &amp;equiv; 0 (mod 4).
&lt;/div&gt;
&lt;p&gt;
Let me show how I proved this conjecture.
&lt;/p&gt;
&lt;h3&gt;Proof of the necessity of the condition n &amp;equiv; 3 (mod 4) or n
&amp;equiv; 0 (mod 4)&lt;/h3&gt;
&lt;p&gt;
Let (1, 1, 2, 2, &amp;hellip;, n, n) be a sequence which can be rearranged
such that there are k numbers between the two k's for each k in the
sequence. Let us number the position of each number in this permutation
as 1,&amp;nbsp;2,&amp;nbsp;&amp;hellip;,&amp;nbsp;2n.
&lt;/p&gt;&lt;p&gt;
Partition the numbers in this partition into two halves:
&lt;ul&gt;
&lt;li&gt;Partition 1: All numbers in odd numbered positions.&lt;/li&gt;
&lt;li&gt;Partition 2: All numbers in even numbered positions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The length of each partition is n.
&lt;/p&gt;&lt;p&gt;
Note that if k (1&amp;nbsp;&amp;le;&amp;nbsp;k&amp;nbsp;&amp;le;&amp;nbsp;n) is even, one of the
two k's is located on an odd numbered position and the other one on even
numbered position.
&lt;/p&gt;&lt;p&gt;
If k is odd the two k's are both located on odd numbered positions or
both located on even numbered positions. The two k's will never be split
in two different partitions. It will be together in one partition.
&lt;/p&gt;&lt;p&gt;
From the above discussion it is obvious that the number of even numbers
in each partition is equal. Let the number of even numbers in each
partition be m. Let us assume there are p pairs of odd numbers in
partition 1 and q pairs of odd numbers in partition 2. Thus, there are
2p odd numbers in partition 1 and 2q odd numbers in partition 2.
&lt;/p&gt;&lt;p&gt;
Since, the length of each partition is n, n&amp;nbsp;=&amp;nbsp;m&amp;nbsp;+&amp;nbsp;2p
= m&amp;nbsp;+&amp;nbsp;2q. Hence, p&amp;nbsp;=&amp;nbsp;q. This implies that there must
be p&amp;nbsp;+&amp;nbsp;q&amp;nbsp;=&amp;nbsp;2p pairs of odd numbers in the two
partitions, i.e. the number of positive odd numbers less than or equal
to n is 2p.
&lt;/p&gt;&lt;p&gt;
In other words, the number of positive odd numbers less than or equal to
n must be even.&amp;nbsp;&lt;b&gt;&amp;hellip;&amp;nbsp;(1)&lt;/b&gt;
&lt;/p&gt;&lt;p&gt;
We know that, for a positive integer n, the number of positive odd
numbers less than or equal to n is &amp;lceil;n/2&amp;rceil;. Let q be the
quotient and r be the remainder obtained after dividing n by 4. We have
four possible cases.
&lt;ol&gt;&lt;li&gt;If n = 4q + 1, number of positive odd numbers less than or equal
to n is, &amp;lceil;(4q + 1)/2&amp;rceil; = 2q + 1.&lt;/li&gt;&lt;li&gt;If n = 4q + 2,
number of positive odd numbers less than or equal to n is, &amp;lceil;(4q +
2)/2&amp;rceil; = 2q + 1.&lt;/li&gt;&lt;li&gt;If n = 4q + 3, number of positive odd
numbers less than or equal to n is, &amp;lceil;(4q + 3)/2&amp;rceil; = 2q +
2.&lt;/li&gt;&lt;li&gt;If n = 4q + 4, number of positive odd numbers less than or
equal to n is, &amp;lceil;(4q + 4)/2&amp;rceil; = 2q + 2.&lt;/li&gt;&lt;/ol&gt;We can see
that the number of odd numbers less than or equal to n is even if and
only if n&amp;nbsp;&amp;equiv;&amp;nbsp;3&amp;nbsp;(mod&amp;nbsp;4) or
n&amp;nbsp;&amp;equiv;&amp;nbsp;0&amp;nbsp;(mod&amp;nbsp;4).&amp;nbsp;&lt;b&gt;&amp;hellip;&amp;nbsp;(2)&lt;/b&gt;
&lt;/p&gt;&lt;p&gt;
From (1) and (2), the necessity of the condition n &amp;equiv; 3 (mod 4) or
n &amp;equiv; 0 (mod 4) is proven.
&lt;/p&gt;
&lt;h3&gt;Proof of the sufficiency of the condition n &amp;equiv; 3 (mod 4) or n
&amp;equiv; 0 (mod 4):&lt;/h3&gt;
&lt;p&gt;
If we can show that we can construct a permutation that satisfies the
condition in the question for every positive integer n such that n
&amp;equiv; 3 (mod 4) or n &amp;equiv; 0 (mod 4), then the proof would be
complete. We have two cases to deal with:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;n &amp;equiv; 3 (mod 4)&lt;/li&gt;
&lt;li&gt;n &amp;equiv; 0 (mod 4)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
When, n&amp;nbsp;&amp;equiv;&amp;nbsp;3&amp;nbsp;(mod&amp;nbsp;4), let
x&amp;nbsp;=&amp;nbsp;(n&amp;nbsp;+&amp;nbsp;1)&amp;nbsp;/&amp;nbsp;4. Partition the first n
natural numbers in 4 sequences, p&lt;sub&gt;1&lt;/sub&gt;, p&lt;sub&gt;2&lt;/sub&gt;,
p&lt;sub&gt;3&lt;/sub&gt; and p&lt;sub&gt;4&lt;/sub&gt; such that each sequence contains
x&amp;nbsp;&amp;minus;&amp;nbsp;1 numbers and 3 single numbers a, b and c according
to the following rules. This is possible since, 4(x&amp;nbsp;&amp;minus;&amp;nbsp;1)
+ 3 = 4x&amp;nbsp;&amp;minus;&amp;nbsp;1 = n + 1&amp;nbsp;&amp;minus;&amp;nbsp;1 = n. The
partitioning rules are as follows.
&lt;ol&gt;
&lt;li&gt;a = 2x&amp;nbsp;&amp;minus;&amp;nbsp;1&lt;/li&gt;&lt;li&gt;b = 4x&amp;nbsp;&amp;minus;&amp;nbsp;2&lt;/li&gt;
&lt;li&gt;c = 4x&amp;nbsp;&amp;minus;&amp;nbsp;1&lt;/li&gt;&lt;li&gt;p&lt;sub&gt;1&lt;/sub&gt; = Odd numbers
between 1 and a in ascending order. (1, &amp;hellip;,
2x&amp;nbsp;&amp;minus;&amp;nbsp;3).&lt;/li&gt;
&lt;li&gt;p&lt;sub&gt;2&lt;/sub&gt; = Even numbers between 1 and a in ascending order. (2,
&amp;hellip;, 2x&amp;nbsp;&amp;minus;&amp;nbsp;2).&lt;/li&gt;
&lt;li&gt;p&lt;sub&gt;3&lt;/sub&gt; = Odd numbers between a and b in ascending order.
(2x&amp;nbsp;+&amp;nbsp;1, &amp;hellip;, 4x&amp;nbsp;&amp;minus;&amp;nbsp;3).&lt;/li&gt;
&lt;li&gt;p&lt;sub&gt;4&lt;/sub&gt; = Even numbers between a and b in ascending order.
(2x, &amp;hellip;, 4x&amp;nbsp;&amp;minus;&amp;nbsp;4).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
It can be verified that the above
partitioning rules imply that the length of each partition p&lt;sub&gt;i&lt;/sub&gt;
is x&amp;nbsp;&amp;minus;&amp;nbsp;1 where 1 &amp;le; i &amp;le; 4. Also, all natural
numbers less than or equal to n are present in the 4 partitions and 3
single numbers above.
&lt;/p&gt;&lt;p&gt;
Let us consider an array A of size 2n. The first index of array is 1.
Now, a valid permutation can be formed with the following placement
rules.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A[1] to A[x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains reversed
p&lt;sub&gt;4&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[x] to A[2x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains reversed
p&lt;sub&gt;1&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[2x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains b.&lt;/li&gt;
&lt;li&gt;A[2x] to A[3x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains p&lt;sub&gt;1&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[3x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains c.&lt;/li&gt;
&lt;li&gt;A[3x] to A[4x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains p&lt;sub&gt;4&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[4x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains a.&lt;/li&gt;
&lt;li&gt;A[4x] to A[5x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains reversed
p&lt;sub&gt;3&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[5x&amp;nbsp;&amp;minus;&amp;nbsp;1] to A[6x&amp;nbsp;&amp;minus;&amp;nbsp;3] contains
reversed p&lt;sub&gt;2&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[6x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains b.&lt;/li&gt;
&lt;li&gt;A[6x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains a.&lt;/li&gt;
&lt;li&gt;A[6x] to A[7x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains p&lt;sub&gt;2&lt;/sub&gt;.&lt;/li&gt;
&lt;li&gt;A[7x&amp;nbsp;&amp;minus;&amp;nbsp;1] contains c.&lt;/li&gt;
&lt;li&gt;A[7x] to A[8x&amp;nbsp;&amp;minus;&amp;nbsp;2] contains p&lt;sub&gt;3&lt;/sub&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Note that 8x&amp;nbsp;&amp;minus;&amp;nbsp;2 = 2n. So, the
last rule can be rewritten as &quot;A[7x] to A[2n] contains p&lt;sub&gt;3&lt;/sub&gt;&quot;.
&lt;/p&gt;&lt;p&gt;
When n &amp;equiv; 0&amp;nbsp;(mod&amp;nbsp;4), let x&amp;nbsp;=&amp;nbsp;n&amp;nbsp;/&amp;nbsp;4.
Partition the first natural numbers in 4 sequences, p&lt;sub&gt;1&lt;/sub&gt;,
p&lt;sub&gt;2&lt;/sub&gt;, p&lt;sub&gt;3&lt;/sub&gt; and p&lt;sub&gt;4&lt;/sub&gt; such that each sequence
contains x&amp;nbsp;&amp;minus;&amp;nbsp;1 numbers and 4 single numbers a, b, c and
d. The partition rules contain the 7 rules discussed above and one more
rule. The additional rule is: d&amp;nbsp;=&amp;nbsp;4x.
&lt;/p&gt;&lt;p&gt;
The placement are same for all cells in the array with one exception and
two additional rules for the two new cells.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A[4x&amp;nbsp;&amp;minus;&amp;nbsp;1] = d. (Exception. Modifies rule 7.)&lt;/li&gt;
&lt;li&gt;A[8x&amp;nbsp;&amp;minus;&amp;nbsp;1] = a.&lt;/li&gt;
&lt;li&gt;A[8x] = d.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
With these rules it can be shown that two k's have k elements in
between. In other words, the difference in the array indices of the
cells where two k's are present is k&amp;nbsp;+&amp;nbsp;1.
&lt;/p&gt;&lt;p&gt;
a = 2x&amp;nbsp;&amp;minus;&amp;nbsp;1. The two a's are located at
A[4x&amp;nbsp;&amp;minus;&amp;nbsp;1] and A[6x&amp;nbsp;&amp;minus;&amp;nbsp;1]. The difference
in array indices is 2x. Hence, two a's have 2x&amp;nbsp;&amp;minus;&amp;nbsp;1 = a
numbers in between. Similarly, this can be verified for b, c and d.
&lt;/p&gt;&lt;p&gt;
The two 1's are located at A[2x&amp;nbsp;&amp;minus;&amp;nbsp;2] and A[2x]. The
difference in array indices is 2. Hence, the two 1's have 1 number in
between. From this it can be shown that for each k in p&lt;sub&gt;1&lt;/sub&gt;,
there are k numbers in between. Take an arbitrary element m in
p&lt;sub&gt;1&lt;/sub&gt;. From the construction rules, we see that, the two m's are
located in
A[2x&amp;nbsp;&amp;minus;&amp;nbsp;2&amp;nbsp;&amp;minus;&amp;nbsp;(m&amp;nbsp;&amp;minus;&amp;nbsp;1)/2]
and A[2x + (m&amp;nbsp;&amp;minus;&amp;nbsp;1)/2]. The difference in the indices is:
2x + (m&amp;nbsp;&amp;minus;&amp;nbsp;1)/2&amp;nbsp;&amp;minus;&amp;nbsp;2x + 2 +
(m&amp;nbsp;&amp;minus;&amp;nbsp;1)/2 = m + 1. Similarly, it can be shown that for
any k in any parition p&lt;sub&gt;i&lt;/sub&gt; (1 &amp;le; i &amp;le; 4), there are k
numbers between the two k's.
&lt;/p&gt;&lt;p&gt;
Here is an example.
&lt;/p&gt;
&lt;pre&gt;
n = 11
x = (11 + 1) / 4 = 3
a = 5
b = 10
c = 11
p1 = (1, 3)
p2 = (2, 4)
p3 = (7, 9)
p4 = (6, 8)
&lt;/p&gt;&lt;p&gt;
                      (a)
                      +------------+
                (c)   |            |
                +-----|------------|------+
         (b)    |     |            |      |
         +------|-----|----------+ |      |
         |      |     |          | |      |
8 6 3 1 10 1 3 11 6 8 5 9 7 4 2 10 5 2 4 11 7 9
=== ===    ===    ===   === ===      ===    ===
 |   |      |      |     |   |        |      |
 |   +------+ (p1) |     |   +--------+ (p2) |
 +-----------------+     +-------------------+
                  (p4)                      (p3
&lt;/pre&gt;
&lt;h3&gt;Notes&lt;/h3&gt;
&lt;ol class=&quot;paralist&quot;&gt;
&lt;li&gt;There is an entry for this problem in OEIS: &lt;a
href=&quot;http://oeis.org/A014552&quot;&gt;http://oeis.org/A014552&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;The internal symmetries in the permutation are not very obvious from
the proof but the permutation simply requires us to place a, b, c,
p&lt;sub&gt;1&lt;/sub&gt;, p&lt;sub&gt;2&lt;/sub&gt; and p&lt;sub&gt;3&lt;/sub&gt; in this order:
(p&lt;sub&gt;4&lt;/sub&gt;' p&lt;sub&gt;1&lt;/sub&gt;' b p&lt;sub&gt;1&lt;/sub&gt; c p&lt;sub&gt;4&lt;/sub&gt; a
p&lt;sub&gt;3&lt;/sub&gt;' p&lt;sub&gt;2&lt;/sub&gt;' b a p&lt;sub&gt;2&lt;/sub&gt; c p&lt;sub&gt;3&lt;/sub&gt;) when n
&amp;equiv; 3 (mod 4), where p&lt;sub&gt;i&lt;/sub&gt;' is reversed p&lt;sub&gt;i&lt;/sub&gt; (1
&amp;le; i &amp;le; 4).&lt;/li&gt;
&lt;li&gt;For n = 3, we get x = 1. So, length of the partitions p&lt;sub&gt;1&lt;/sub&gt;,
p&lt;sub&gt;2&lt;/sub&gt;, p&lt;sub&gt;3&lt;/sub&gt; and p&lt;sub&gt;4&lt;/sub&gt; is x&amp;nbsp;&amp;minus;&amp;nbsp;1
= 0 each. So, rules that involve the partitions do not apply in case of
n = 3.&lt;/li&gt;
&lt;li&gt;In this proof, I have shown one set of rules to construct a valid
permutation for a given n such that n &amp;equiv; 3 (mod 4) or n &amp;equiv; 0
(mod 4). But actually, I found 2 such sets of rules. The variables a, b,
c and d remain same in the other rule. Only the rules for&amp;nbsp;
p&lt;sub&gt;1&lt;/sub&gt;, p&lt;sub&gt;2&lt;/sub&gt;, p&lt;sub&gt;3&lt;/sub&gt;, p&lt;sub&gt;4&lt;/sub&gt; and placement
rules differ.&lt;/li&gt;
&lt;li&gt;The above two points imply that, except for n = 3, using
construction rules, we can construct at least two different permutations
for a given value of n such that n &amp;equiv; 3 (mod 4) or n &amp;equiv; 0 (mod
4).&lt;/li&gt;
&lt;li&gt;It seemed like for values of n &amp;gt; 20, there might be over a
quadrillion possible solutions. It wasn't feasible to verify this on my
laptop with the program I wrote. When I discussed this with a person who
was interested in this problem, he asked, &quot;What? Don't you have a
quad(rillion)-core processor?&quot;&lt;/li&gt;
&lt;li&gt;I worked through a Saturday night till 3:30 AM of the following
Sunday morning and I could prove only the necessity of the condition n
&amp;equiv; 3 (mod 4) or n &amp;equiv; 0 (mod 4).  When &lt;a
href=&quot;http://anks007.blogspot.com/&quot;&gt;a friend&lt;/a&gt; asked me about the
progress, I could manage to say this, &quot;Half of it. Need to prove a
'necessary and sufficient' condition. Proved the necessary part only but
this much is sufficient for me today. The sufficient part is necessary
for the proof to be complete but i think, now it has become necessary to
do the sufficient part tomorrow as it is too late already.&quot;&lt;/li&gt;
&lt;li&gt;The above point reminds me of a famous quote by &lt;a
href=&quot;http://en.wikipedia.org/wiki/George_Pólya&quot;&gt;George Pólya&lt;/a&gt;,
&quot;Mathematics consists in proving the most obvious thing in the least
obvious way&quot;.&lt;/li&gt;
&lt;li&gt;The above point reminds me of &lt;a
href=&quot;http://humor.beecy.net/misc/principia/&quot;&gt;http://humor.beecy.net/misc/principia/&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;        </description>
        <pubDate>Sat, 17 Sep 2011 12:45:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/langford-pairing/</guid>
    </item>
    <!-- #orkut-exploit - The Orkut exploit -->
    <item>
        <title>The Orkut exploit</title>
        <link>http://susam.in/blog/orkut-exploit/</link>
        <description>
            &lt;p&gt;
I first came to know about Sunny Vaghela about one and a half years ago
when I got an email from &lt;a
href=&quot;http://blogs.oracle.com/sandip/&quot;&gt;Sandip Dev&lt;/a&gt; of Sun
Microsystems (now acquired by Oracle).
&lt;/p&gt;

&lt;div class=&quot;email&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;p&gt;
&lt;b&gt;From:&lt;/b&gt; &quot;Sandip Dev&quot;&lt;br&gt;
&lt;b&gt;To:&lt;/b&gt; &quot;Susam Pal&quot;&lt;br&gt;
&lt;b&gt;Date:&lt;/b&gt; Thu, Dec 10, 2009 12:23 AM IST&lt;br&gt;
&lt;b&gt;Subject:&lt;/b&gt; The Orkut exploit
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;text&quot;&gt;
&lt;p&gt;
Hi Susam,
&lt;/p&gt;&lt;p&gt;
I just read the Orkut exploit on your site &lt;a
href=&quot;http://susam.in/security/advisory-2007-06-22.txt&quot;&gt;http://susam.in/security/advisory-2007-06-22.txt&lt;/a&gt;.
It seems this guy, Sunny Vaghela, claims this exploit to be his own (&lt;a
href=&quot;http://sunnyvaghela.com/orkut-hacking.html&quot;&gt;http://sunnyvaghela.com/orkut-hacking.html&lt;/a&gt;).
He also claims that people from Google visited him (&lt;a
href=&quot;http://www.techgoss.com/Story/227S12-Security-expert-starts-NGO-to-help-cyber-victims.aspx&quot;&gt;http://www.techgoss.com/Story/227S12-Security-expert-starts-NGO-to-help-cyber-victims.aspx&lt;/a&gt;).
Whats your take on this?
&lt;/p&gt;&lt;p&gt;
Regards,&lt;br&gt;
Sandip Dev
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;email&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;p&gt;
&lt;b&gt;From:&lt;/b&gt; &quot;Susam Pal&quot;&lt;br&gt;
&lt;b&gt;To:&lt;/b&gt; &quot;Sandip Dev&quot;&lt;br&gt;
&lt;b&gt;Date:&lt;/b&gt; Thu, Dec 10, 2009 1:17 AM IST&lt;br&gt;
&lt;b&gt;Subject:&lt;/b&gt; Re: The Orkut exploit&lt;br&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;text&quot;&gt;
&lt;p&gt;
Hi Sandeep,
&lt;/p&gt;&lt;p&gt;
In our advisory, we have documented that the session management
vulnerability associated with orkut_session cookie was first reported by
Net-Square Solutions Private Limited. We published an advisory in 2007
because even though the Net-Square advisory mentioned that the
vulnerability is fixed, we found that it wasn't. So, we published the
results of our investigation and experiments.
&lt;/p&gt;&lt;p&gt;
The link that you have sent me doesn't mention that he claims the
exploit to be his own. I have not seen the Headline Today video on his
work. However, if he does claim that it is his own exploit, either he
has discovered the vulnerability independently and is unaware of related
work that has been done before or he is using information from the
advisories published by us and Net-Square, but claiming it to be his
own.
&lt;/p&gt;&lt;p&gt;
Regards,&lt;br&gt;
Susam Pal
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;email&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;p&gt;
&lt;b&gt;From:&lt;/b&gt; &quot;Sandip Dev&quot;&lt;br&gt;
&lt;b&gt;To:&lt;/b&gt; &quot;Susam Pal&quot;&lt;br&gt;
&lt;b&gt;Date:&lt;/b&gt; Thu, Dec 10, 2009 9:29 AM IST&lt;br&gt;
&lt;b&gt;Subject:&lt;/b&gt; Re: The Orkut exploit&lt;br&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;text&quot;&gt;
&lt;p&gt;
Hi Susam,
&lt;/p&gt;&lt;p&gt;
He has put it under &quot;Research&quot; on his site and also in the interview he
says he &quot;found&quot; this exploit. Well I have reading up about him recently
and this came up. So I thought I would alert you. Thanks for the
response.
&lt;/p&gt;&lt;p&gt;
Regards,&lt;br&gt;
Sandip
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;email&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;p&gt;
&lt;b&gt;From:&lt;/b&gt; &quot;Susam Pal&quot;&lt;br&gt;
&lt;b&gt;To:&lt;/b&gt; &quot;Sandip Dev&quot;&lt;br&gt;
&lt;b&gt;Date:&lt;/b&gt; Thu, Dec 10, 2009 10:18 AM IST&lt;br&gt;
&lt;b&gt;Subject:&lt;/b&gt; Re: The Orkut exploit&lt;br&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;text&quot;&gt;
&lt;p&gt;
Hi Sandip,
&lt;/p&gt;&lt;p&gt;
Thanks for the alert. I was assuming that he was talking about his work
in good faith. There is a possibility, no matter however small, that he
has discovered the vulnerability independently and he is not aware that
we have already investigated this and published advisories on it.
&lt;/p&gt;&lt;p&gt;
However, I understand that there is also a possibility that he is
violating our copyright on our work and claiming that the work is a
result of his own research. I don't mind it since most websites on the
web properly attribute the advisory and the investigation to me and
Vipul.
&lt;/p&gt;&lt;p&gt;
Thanks for the alert however. It was interesting to see an old work in
the news recently.
&lt;/p&gt;&lt;p&gt;
Regards,&lt;br&gt;
Susam Pal
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
A couple of days ago, I found him again at &lt;a
href=&quot;http://attrition.org/&quot;&gt;attrition.org&lt;/a&gt;, a website that used to
be the largest mirror of defaced websites.
&lt;/p&gt;&lt;p&gt;
Sunny Vaghela has found a place in attrition.org's &lt;a
href=&quot;http://attrition.org/errata/charlatan/&quot;&gt;charlatans&lt;/a&gt; &lt;a
href=&quot;http://attrition.org/errata/charlatan/watch_list/&quot;&gt;watch list&lt;/a&gt;:
&lt;a
href=&quot;http://attrition.org/errata/charlatan/watch_list/sunny_vaghela/orkut-claims.html&quot;&gt;Sunny
Vaghela: Claims of Orkut Vulnerability Research&lt;/a&gt;. He is the third
Indian to get into this list after &lt;a
href=&quot;http://attrition.org/errata/charlatan/ankit_fadia/&quot;&gt;Ankit
Fadia&lt;/a&gt; and &lt;a
href=&quot;http://attrition.org/errata/charlatan/watch_list/sahil_khan/hackers_and_crackers.html&quot;&gt;Sahil
Khan&lt;/a&gt;.
&lt;/p&gt;        </description>
        <pubDate>Sat, 25 Jun 2011 11:03:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/orkut-exploit/</guid>
    </item>
    <!-- #urls-in-c - URLs in C -->
    <item>
        <title>URLs in C</title>
        <link>http://susam.in/blog/urls-in-c/</link>
        <description>
            &lt;p&gt;
Here is a C puzzle I like to ask my friends and colleagues. It is one of
those silly puzzles that may bring a smile on your face if you aren't
able to figure it out immediately.
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
#include &amp;lt;stdio.h&amp;gt;

int main()
{
    http://blog.susam.in/2011/06/urls-in-c.html
    printf(&quot;URLs in C\n&quot;);
    return 0;
}
&lt;/pre&gt;
&lt;p&gt;
This code compiles and runs successfully even though the &lt;a
href=&quot;http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf&quot;&gt;C99
standard&lt;/a&gt; doesn't mention anywhere that URLs are valid syntactic
elements in C. How does this code work then?
&lt;/p&gt;        </description>
        <pubDate>Fri, 03 Jun 2011 10:13:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/urls-in-c/</guid>
    </item>
    <!-- #infosys-tcs-or-wipro - Infosys, TCS, or Wipro? -->
    <item>
        <title>Infosys, TCS, or Wipro?</title>
        <link>http://susam.in/blog/infosys-tcs-or-wipro/</link>
        <description>
            &lt;p&gt;
Sometimes computer science, IT or electronic and communication
engineering students get placed in two or three major Indian IT
companies and they find it hard to decide which one to join. &quot;Infosys,
TCS, or Wipro?&quot; is one of the most common questions I have faced from such
students. The answer is much simpler than they think it is.
&lt;/p&gt;&lt;p&gt;
&quot;None.&quot;
&lt;/p&gt;&lt;p id=&quot;what-this-post-is-not&quot;&gt;
This blog post is not about how these companies feed the stomachs of
lakhs of people. This blog post is not about undermining the efforts of
these companies. They are probably good at keeping their customers
happy. This blog is not about offending the employees of these
organizations. That'll be an unintentional side-effect.
&lt;/p&gt;&lt;p&gt;
This blog post is about a choice that freshers usually have to make and
the information they should have before they make the choice. This blog
post is about urging the freshers who want to make a career in
engineering to not make a mistake that I did because I did not have the
necessary information at the right time; a mistake that I could correct
two years later after I realized it. This blog post is about some very
unpleasant facts about these major Indian IT companies that you wouldn't
know unless you have been a part of it.
&lt;/p&gt;

&lt;p id=&quot;engineering-alert&quot; class=&quot;warning&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;ALERT:&lt;/span&gt; If you are not interested in
making a career in engineering, lack the confidence to do so, or you are
very content with working for one of these three companies for reasons
that are valid to you, this post is not for you. It won't make any sense
to you. Do not proceed.
&lt;/p&gt;

&lt;p&gt;
Now, let me start slaying the different myths that exist about these
organizations one by one and I am not going to mince words while doing
this. You have been warned.
&lt;/p&gt;
&lt;ol class=&quot;paralist&quot;&gt;
&lt;li&gt;
&lt;b&gt;Training:&lt;/b&gt; People think that these organizations are good for
freshers because they get a lot of training which they wouldn't get in
other organizations. I must remind such people that attending training
programmes is not equivalent to learning. Indeed these organizations
provide a lot of training to freshers but only about 1% of the trainees
actually absorb the knowledge. The 1% that do absorb the training do not
stick to the organization for a long time because sooner or later they
realize that they want to do some real engineering.  The figure '1%'
isn't merely a guess. This is my observation across various
trainee-batches that have been trained in one of these organizations.
Think about it.  Can you learn a new programming language in just 3
days? If your answer is &quot;no&quot;, you shouldn't join one of these
organizations. If your answer is &quot;yes&quot;, you shouldn't join one of these
organizations.
&lt;/li&gt;&lt;li&gt;
&lt;b&gt;Engineering:&lt;/b&gt; One can find engineering
problems in these organizations but no trace of engineering. For those
of you who work in one of these organizations and are offended by this
statement, please go and open your engineering textbooks again. Try to
remind yourself what you studied and what you learnt. Consider what you
do now.
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Engineers:&lt;/b&gt; The number of engineers in these organizations are
very very few; perhaps only 1 in every 200 is an engineer. This is a
guess, albeit not a wild one. This is why there is no engineering in
these companies despite the presence of engineering problems. &quot;But isn't
the minimum qualification to get a job in one of these organizations
bachelor's of engineering?&quot;, you might ask. It is.  Yes, all of them
have a degree in engineering or computers of some sort but only about 1
out of 200 is an engineer. The rest 199 do not understand why a bitcount
of 1's complement of bitwise XOR of two variables would give you the
number of similar bits in corresponding positions in both variables, why
one can not create a POSIX compliant regular expression to match only
strings with balanced parentheses, or how to find the shortest chain of
connections between two friends in a social network. Note that I have
used 'or' as the conjunction and not 'and'.  They may be good software
users or good &quot;software-tailors&quot; who can create software by stitching
together many library functions but they aren't engineers.
&lt;/li&gt;&lt;li&gt;
&lt;b&gt;Culture:&lt;/b&gt;
One of the worst cultures you can find in the whole of software
industry. Very few are busy trying to learn a few things mentioned in
the previous paragraph. Some employees are busy figuring out ways to
impress their female colleagues using the resources provided by the
organization rather than learning and solving problems in better ways.
Others are busy cribbing. Here is a shocking piece of information for
those who have never worked for one of these organizations. One can also
manage to find mud-slinging in company forums once in a while.
Professionalism is at its worst here. But they convince themselves that
they are professional because they speak English fluently and know how
to wear a tie. Employees feel their salaries are pathetic. I feel they
are overpaid. How much should a good software user earn?
&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Onsite:&lt;/b&gt; Contrary to the popular belief, the number of trips to
foreign lands isn't a measure of one's technical prowess. It is mostly
(but not always) a measure of how dispassionate one is about engineering
and his profession, and how greedy one can be for wealth.  Some of the
best engineers I have met in these organizations were never eager to go
onsite, never went, joined an organization where they could put their
knowledge and skills to better use and then flew to a foreign land
because their knowledge, skills and understanding of technology were
needed there.
&lt;/li&gt;
&lt;/ol&gt;
&lt;p id=&quot;suggestion&quot;&gt;
So, my answer to the question &quot;Infosys, TCS, or Wipro?&quot; is &quot;None.&quot;
That's not very helpful. Here is a more helpful one. One can consider
applying for a job in an organization where he or she can get an
opportunity to solve some engineering problems. One cannot learn
engineering and programming merely by attending trainings. One has to
learn it by doing, solving problems, observing what experienced
engineers do, experimenting, screwing up a few times and reworking,
talking to good engineers, etc. One can try looking for an organization
where the leaders of projects are very good engineers. Start-ups are
more likely to have them. Some matured ones are
&lt;a href=&quot;http://www.gluster.com/&quot;&gt;Gluster&lt;/a&gt;,
&lt;a href=&quot;http://www.parallocity.com/&quot;&gt;Parallocity&lt;/a&gt;,
&lt;a href=&quot;http://www.slideshare.net/&quot;&gt;SlideShare&lt;/a&gt;, etc. New start-ups
come up every year. Software companies which develop famous and
successful products are more likely to have them. Some good examples are
&lt;a href=&quot;http://www.adobe.com/&quot;&gt;Adobe&lt;/a&gt;,
&lt;a href=&quot;http://www.amazon.com/&quot;&gt;Amazon&lt;/a&gt;,
&lt;a href=&quot;http://www.google.com/&quot;&gt;Google&lt;/a&gt;,
&lt;a href=&quot;http://www.phoenix.com/&quot;&gt;Phoenix&lt;/a&gt;,
&lt;a href=&quot;http://www.rsa.com/&quot;&gt;RSA&lt;/a&gt;,
etc. So, how does one
figure whether a certain organization is an organization of engineers or
an organization of good software users?
&lt;/p&gt;&lt;p&gt;
The clue is: Interview.
&lt;/p&gt;&lt;p&gt;
Remember the questions they ask in the interview. Think about them
later. Try discussing the questions with your friends who are known for
solving tough engineering problems. An interview is not only an
opportunity for an organization to evaluate an applicant, it is also an
opportunity for the applicant to evaluate an organization. 
&lt;/p&gt;
&lt;p class=&quot;update&quot;&gt;
&lt;b&gt;Update:&lt;/b&gt; October 28, 2011: This post got way more attention
than I wanted. It was discussed and debated &lt;a
href=&quot;http://hackerstreet.in/item?id=6323&quot;&gt;at&lt;/a&gt;&amp;nbsp;&lt;a
href=&quot;http://news.ycombinator.com/item?id=2628318&quot;&gt;various&lt;/a&gt;&amp;nbsp;&lt;a
href=&quot;http://www.reddit.com/r/india/comments/lqemo/why_a_fresher_should_not_join_infosys_tcs_or_wipro/&quot;&gt;places&lt;/a&gt;
on the web. As a result, here is a follow-up post: &lt;a
href=&quot;/blog/re-infosys-tcs-or-wipro/&quot;&gt;Re: Infosys, TCS, or Wipro?&lt;/a&gt;
&lt;/p&gt;        </description>
        <pubDate>Sun, 15 May 2011 08:45:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/infosys-tcs-or-wipro/</guid>
    </item>
    <!-- #surprise-test - A surprise test -->
    <item>
        <title>A surprise test</title>
        <link>http://susam.in/blog/surprise-test/</link>
        <description>
            &lt;p&gt;
I was studying in the 1st standard. Our science teacher gave us a
surprise test. I tried to answer all questions correctly. After the
corrected notebooks were returned, I found that the teacher had
incorrectly marked one of my answers as incorrect. I was sure that I was
correct. So, I showed it to the girl sitting next to me and declared
innocently, &quot;This answer is correct. But she doesn't know. She is
stupid.&quot;
&lt;/p&gt;&lt;p&gt;
When she went to collect her notebook, she informed the teacher about
what I had just said. The teacher was offended and asked me to stand
outside the class. Our principal was passing through the corridor and
she was surprised to see me standing outside. She asked what I did and I
said that I called our teacher stupid. She asked me to apologize.
&lt;/p&gt;&lt;p&gt;
I didn't know how to do that. I started recollecting from Hindi movies.
The villian would fall at someone's feet and cry, &quot;Mujhe maaf kar do.
Please, mujhe maaf kar do.&quot;&lt;sup&gt;&lt;a href=&quot;#translate&quot;&gt;[*]&lt;/a&gt;&lt;/sup&gt; I
thought, &quot;I can't do this. I can't fall at her feet. That would be very
embarrassing. I said that she was stupid and I was right. Why should I
fall at her feet?&quot; I refused to apologize, inspite of repeated warnings
and requests. I was simply not ready to fall at her feet and do the
drama.
&lt;/p&gt;&lt;p&gt;
The matter escalated and I was asked to leave that place. I was taken to
the reception area. I was not allowed to attend any classes. I sat there
crying the whole day. Visitors came and enquired why I was crying. I
explained. Before the final bell rang, a complaint was registered in my
diary and my parents were asked to meet the principal next day.
&lt;/p&gt;&lt;p&gt;
The next day, my parents met the principal. Thankfully, it went well.
She explained how she was impressed with my innocence and honesty. It
seemed as if she had called my parents only to praise me. I grew up to
be wiser and humbler. Now, I understand that falling at someone's feet
is not the only way to apologize.
&lt;/p&gt;&lt;p&gt;
&lt;small&gt;[*] &lt;a name=&quot;translate&quot; id=&quot;translate&quot;&gt;Translated to English:
&quot;Forgive me. Please, forgive me.&quot;&lt;/a&gt;&lt;/small&gt;
&lt;/p&gt;        </description>
        <pubDate>Sun, 08 May 2011 08:46:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/surprise-test/</guid>
    </item>
    <!-- #from-out-shuffles-to-multiplicative-order - From out shuffles to multiplicative order -->
    <item>
        <title>From out shuffles to multiplicative order</title>
        <link>http://susam.in/blog/from-out-shuffles-to-multiplicative-order/</link>
        <description>
            &lt;p&gt;
A colleague asked this problem.
&lt;div class=&quot;problem&quot;&gt;
A perfect riffle shuffle or bridge shuffle is when we split a deck of
cards into two halves (one in each hand), and then alternately drop one
card from each half till all have fallen. For the sake of this problem,
let us assume that the top card will always be the top card and the
bottom card will always be the bottom card after shuffling. However the
rest of the cards will be deranged.
&lt;/p&gt;&lt;p&gt;
How many such riffle shuffles do we need to perform on a deck of 52
cards, in order to obtain the exact same starting order?
&lt;/div&gt;
Such a style of shuffling is also known as &lt;em&gt;out shuffle&lt;/em&gt;.
&lt;/p&gt;&lt;p&gt;
Let us assume that there are 2n cards where n is a positive integer. Let
us number the positions of the cards as
0,&amp;nbsp;1,&amp;nbsp;&amp;hellip;&amp;nbsp;2n&amp;nbsp;&amp;minus;&amp;nbsp;1 where position 0 is
the top of the deck and position 2n &amp;minus; 1 is the bottom of the deck.
Here is an example with 10 cards called c&lt;sub&gt;0&lt;/sub&gt;, c&lt;sub&gt;1&lt;/sub&gt;,
&amp;hellip; c&lt;sub&gt;9&lt;/sub&gt; with positions indicated in the first column.
&lt;pre&gt;
0. c&lt;sub&gt;0&lt;/sub&gt;                     c&lt;sub&gt;0&lt;/sub&gt;
1. c&lt;sub&gt;1&lt;/sub&gt;                     c&lt;sub&gt;5&lt;/sub&gt;
2. c&lt;sub&gt;2&lt;/sub&gt;                     c&lt;sub&gt;1&lt;/sub&gt;
3. c&lt;sub&gt;3&lt;/sub&gt;                     c&lt;sub&gt;6&lt;/sub&gt;
4. c&lt;sub&gt;4&lt;/sub&gt; &amp;mdash;&amp;mdash;&amp;mdash;&gt; Shuffling &amp;mdash;&amp;mdash;&amp;mdash;&gt; c&lt;sub&gt;2&lt;/sub&gt;
5. c&lt;sub&gt;5&lt;/sub&gt;                     c&lt;sub&gt;7&lt;/sub&gt;
6. c&lt;sub&gt;6&lt;/sub&gt;                     c&lt;sub&gt;3&lt;/sub&gt;
7. c&lt;sub&gt;7&lt;/sub&gt;                     c&lt;sub&gt;8&lt;/sub&gt;
8. c&lt;sub&gt;8&lt;/sub&gt;                     c&lt;sub&gt;4&lt;/sub&gt;
9. c&lt;sub&gt;9&lt;/sub&gt;                     c&lt;sub&gt;9&lt;/sub&gt;
&lt;/pre&gt;
Let us consider a card at position k where 0 &amp;le; k &amp;lt; 2n. It is easy
to see from the description of the problem that a card at position k
moves to position 2k if the card belongs to the top half of the deck,
otherwise it moves to 2(k &amp;minus; n) + 1.
&lt;/p&gt;&lt;p&gt;
2(k &amp;minus; n) + 1 &amp;equiv; 2k &amp;minus; (2n &amp;minus; 1) &amp;equiv; 2k (mod 2n
&amp;minus; 1)
&lt;/p&gt;&lt;p&gt;
So, we can say that a card at position k moves to position k' &amp;equiv; 2k
mod (2n &amp;minus; 1) after an out shuffle where 0 &amp;le; k' &amp;lt; 2n.
&lt;/p&gt;&lt;p&gt;
So, after m shuffles a card at position k moves to position k` &amp;equiv;
2&lt;sup&gt;m&lt;/sup&gt; &amp;middot; k (mod 2n &amp;minus; 1) where 0 &amp;le; k` &amp;lt; 2n. So,
we are looking for the smallest positive integer m such that for all
integers k, where 0 &amp;le; k &amp;lt; 2n,
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;m&lt;/sup&gt; &amp;middot; k &amp;equiv; k (mod 2n &amp;minus; 1)
&lt;/p&gt;&lt;p&gt;
Removing k from both sides we get
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod &amp;nbsp;
&lt;sup&gt;(2n&amp;minus;1)&lt;/sup&gt;&amp;frasl;&lt;sub&gt;gcd(k, 2n&amp;minus;1)&lt;/sub&gt;)
&lt;/p&gt;&lt;p&gt;
For simplicity, let us represent
&lt;sup&gt;(2n&amp;minus;1)&lt;/sup&gt;&amp;frasl;&lt;sub&gt;gcd(k, 2n&amp;minus;1)&lt;/sub&gt;) as
F&lt;sub&gt;nk&lt;/sub&gt;. So, a card at position k returns to the same position
after m out shuffles where m is the smallest positive integer that
satisfies the congruence relation
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod F&lt;sub&gt;nk&lt;/sub&gt;)
&lt;/p&gt;&lt;p&gt;
Since 2n &amp;minus; 1 is odd, F&lt;sub&gt;nk&lt;/sub&gt; must be odd. So, gcd(2,
F&lt;sub&gt;nk&lt;/sub&gt;) = 1. The smallest positive integer m that satisfies the
congruence relation above is known as the multiplicative order of 2
modulo F&lt;sub&gt;nk&lt;/sub&gt; and is denoted as ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;(2).
&lt;/p&gt;&lt;p&gt;
If n = 1, F&lt;sub&gt;nk&lt;/sub&gt; = 1 and 2&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod
F&lt;sub&gt;nk&lt;/sub&gt;) is true for all positive integers m. So, with two cards,
an out shuffle doesn't change the positions of the cards.
&lt;/p&gt;&lt;p&gt;
If n &amp;gt;  2, let us consider a k which is coprime to 2n &amp;minus; 1 and 1
&amp;lt; k &amp;lt; 2n &amp;minus; 1. There exists at least one such k because
&amp;phi;(x) &amp;ge; 2 for x &amp;gt; 2 where &amp;phi;(x) is the Euler's totient of a
positive integer x which is defined as the number of positive integers
less than or equal to x that are coprime to x. Now, gcd(k, 2n &amp;minus; 1)
= 1. So, now the congruence relation
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod &amp;nbsp;
&lt;sup&gt;(2n&amp;minus;1)&lt;/sup&gt;&amp;frasl;&lt;sub&gt;gcd(k, 2n&amp;minus;1)&lt;/sub&gt;)
&lt;/p&gt;&lt;p&gt;
reduces to
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod 2n &amp;minus; 1)
&lt;/p&gt;&lt;p&gt;
So, far we understand that after ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2) out
shuffles, any card at a position number coprime to
2n&amp;nbsp;&amp;minus;&amp;nbsp;1 would be back to its original position. What
about cards at other positions?
&lt;/p&gt;&lt;p&gt;
For a card at position k such that k is not coprime to 2n &amp;minus; 1,
gcd(k, 2n &amp;minus; 1) &amp;ne; 1. In this case the number of out shuffles
required to bring the card at its original position is
ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;2.
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2)&lt;/sup&gt; &amp;equiv; 1 (mod 2n &amp;minus; 1)
&amp;rArr; 2&lt;sup&gt;ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2)&lt;/sup&gt; &amp;equiv; 1 (mod
F&lt;sub&gt;nk&lt;/sub&gt;) &amp;hellip; (since F&lt;sub&gt;nk&lt;/sub&gt; | 2n &amp;minus; 1)
&lt;/p&gt;&lt;p&gt;
As a consequence of Lagrange's theorem, ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;(2)
| ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2).
&lt;/p&gt;&lt;p&gt;
In other words, ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2) = c &amp;middot;
ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;(2) for some positive integer c.
&lt;/p&gt;&lt;p&gt;
So, performing ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2) out shuffles is same as
repeating ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;(2) out shuffles c times. Every
ord&lt;sub&gt;F&lt;sub&gt;nk&lt;/sub&gt;&lt;/sub&gt;(2) shuffles brings back the card at
position k to the same position. So, repeating it c times would leave it
at the same position as well.
&lt;/p&gt;&lt;p&gt;
Hence, we conclude that &lt;em&gt;ord&lt;sub&gt;2n&amp;minus;1&lt;/sub&gt;(2) out shuffles
would restore a deck of 2n cards to its original configuration, where n
is a positive integer.&lt;/em&gt;
&lt;/p&gt;&lt;p&gt;
For a deck of 52 cards, n = 26. So, ord&lt;sub&gt;51&lt;/sub&gt;(2) out shuffles
would restore the deck to its original configuration.
&lt;/p&gt;&lt;p&gt;
Let us compute ord&lt;sub&gt;51&lt;/sub&gt;(2).
&lt;/p&gt;&lt;p&gt;
From Carmichael's theorem, 2&lt;sup&gt;&amp;lambda;(51)&lt;/sup&gt; &amp;equiv; 1 (mod 51),
where &amp;lambda;(n) is the smallest positive integer m such that
a&lt;sup&gt;m&lt;/sup&gt; &amp;equiv; 1 (mod n) for every positive integer a coprime to
and smaller than n.
&lt;/p&gt;&lt;p&gt;
&amp;lambda;(51) = lcm(&amp;lambda;(3), &amp;lambda;(17)) = lcm(2, 16) = 16.
&lt;/p&gt;&lt;p&gt;
Again as a consequence of Lagrange's theorem, ord&lt;sub&gt;51&lt;/sub&gt;(2) |
&amp;lambda;(51). In other words, ord&lt;sub&gt;51&lt;/sub&gt;(2) | 16.
&lt;/p&gt;&lt;p&gt;
Let us check all the factors of 16 and see which one is the
multiplicative order of 2 modulo 51.
&lt;/p&gt;&lt;p&gt;
2&lt;sup&gt;2&lt;/sup&gt; &amp;equiv; 4 (mod 51)
2&lt;sup&gt;4&lt;/sup&gt; &amp;equiv; 16 (mod 51)
2&lt;sup&gt;8&lt;/sup&gt; &amp;equiv; 256 &amp;equiv; 1 (mod 51).
&lt;/p&gt;&lt;p&gt;
&amp;there4; ord&lt;sub&gt;51&lt;/sub&gt;(2) = 8.
&lt;/p&gt;&lt;p&gt;
8 is the answer.
&lt;/p&gt;        </description>
        <pubDate>Wed, 23 Mar 2011 04:05:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/from-out-shuffles-to-multiplicative-order/</guid>
    </item>
    <!-- #listening-to-superimposed-waves - Listening to superimposed waves -->
    <item>
        <title>Listening to superimposed waves</title>
        <link>http://susam.in/blog/listening-to-superimposed-waves/</link>
        <description>
            &lt;p&gt;
Let me share a little experiment I did last night. Listen to the
following audio first. This plays a sine wave of 500 Hz for 2 seconds
followed by another sine wave of 500.1 Hz for 2 more seconds. You can
hear a tick after each second. The 500.1 Hz sine wave starts at the
second tick.
&lt;/p&gt;&lt;p&gt;
&lt;div style=&quot;text-align: center&quot;&gt;&lt;audio tabindex=&quot;0&quot; controls=&quot;controls&quot;&gt;
&lt;source
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz-sequenced.mp3&quot;
type=&quot;audio/mpeg&quot;&gt;
&lt;source
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz-sequenced.ogg&quot;
type=&quot;audio/ogg&quot;&gt;
&lt;embed
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz-sequenced.mp3&quot;
type=&quot;audio/mpeg&quot; autostart=&quot;false&quot; loop=&quot;false&quot; height=&quot;45&quot;&gt;&lt;/embed&gt;
&lt;/audio&gt;&lt;br&gt;
&lt;small&gt;&lt;a
href=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz-sequenced.mp3&quot;&gt;Download
500Hz-500point1Hz-sequenced.mp3&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
&lt;p&gt;
Could you notice any difference in the pitch of the two tones? The
difference in the frequencies is so small that you wouldn't be able to
notice it by hearing.
&lt;/p&gt;&lt;p&gt;
Now, let us see what happens when we superimpose both the waves and
listen to it.
&lt;/p&gt;&lt;p&gt;
&lt;div style=&quot;text-align: center&quot;&gt;&lt;audio tabindex=&quot;0&quot; controls=&quot;controls&quot;&gt;
&lt;source
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz.mp3&quot;
type=&quot;audio/mpeg&quot;&gt;
&lt;source
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz.ogg&quot;
type=&quot;audio/ogg&quot;&gt;
&lt;embed
src=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz.mp3&quot;
type=&quot;audio/mpeg&quot; autostart=&quot;false&quot; loop=&quot;false&quot; height=&quot;45&quot;&gt;&lt;/embed&gt;
&lt;/audio&gt;
&lt;br&gt;
&lt;small&gt;&lt;a
href=&quot;http://susam.in/downloads/files/500Hz-500point1Hz/500Hz-500point1Hz.mp3&quot;&gt;Download
500Hz-500point1Hz.mp3&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;
&lt;p&gt;
You would notice that the loudness decreases rapidly at about 5 seconds
and then returns to full volume by 10 seconds, decreases rapidly again
at 10 seconds and returns to full volume by 15 seconds and so on. In
other words, the loudness decreases every 10 seconds.
&lt;/p&gt;&lt;p&gt;
Let us see why this happens from the graph of the waves. The first wave
can be represented with the function
f(t)&amp;nbsp;=&amp;nbsp;0.4&amp;nbsp;sin&amp;nbsp;(2&amp;pi;&amp;nbsp;&amp;middot;&amp;nbsp;500&amp;nbsp;&amp;middot;&amp;nbsp;t)
and the second one with
g(t)&amp;nbsp;=&amp;nbsp;0.4&amp;nbsp;sin&amp;nbsp;(2&amp;pi;&amp;nbsp;&amp;middot;&amp;nbsp;500.1&amp;nbsp;&amp;middot;&amp;nbsp;t)
&lt;/p&gt;&lt;p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img
src=&quot;http://2.bp.blogspot.com/_CihYmKJVITc/TUngVRlppQI/AAAAAAAAA4s/c9YAJiFWqJY/separate-waves-in-phase.png&quot;
/&gt;
&lt;/div&gt;
&lt;p&gt;
At t = 0 seconds, both the waves look very similar. The crests and
troughs of both the waves appear almost simultaneously. On
superposition, the amplitude almost doubles. The graph of the
superimposed waves is shown below. Note that the amplitude of each wave
in the figure above is 0.4 while that of superimposed waves is almost
0.8 as can be seen below. In fact, it is exactly 0.8 at t = 0 seconds
and then gradually decreases in the region 0 &amp;lt; t &amp;lt; 5.
&lt;/p&gt;&lt;p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img
src=&quot;http://3.bp.blogspot.com/_CihYmKJVITc/TUrF3c1VveI/AAAAAAAAA5s/9FgfwGxU8Uw/superimposed-waves-in-phase.png&quot;
/&gt;&lt;/div&gt;
&lt;p&gt;
The time period of the second wave is slightly shorter than the first
one as the frequency of the second wave is slightly greater than the
first one. So, the two waves are not identical. In fact, the second wave
looks like a very slightly compressed form of the first wave. At about t
= 5 seconds, the difference is obvious.
&lt;/p&gt;&lt;p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img
src=&quot;http://3.bp.blogspot.com/_CihYmKJVITc/TUq0E6hjMKI/AAAAAAAAA5g/dQOls49pgLw/separate-waves-out-of-phase.png&quot;
/&gt;&lt;/div&gt;
&lt;p&gt;
The crests and troughs of both the waves do not appear simultaneously at
around t = 5 seconds. In fact, the crest of one wave and the trough of
the other wave appear simultaneously. This cancels out each other on
superposition and thus the resultant wave has almost no amplitude at
this time.
&lt;/p&gt;&lt;p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img
src=&quot;http://1.bp.blogspot.com/_CihYmKJVITc/TUngWbfMa9I/AAAAAAAAA5E/fs_J8spSYgQ/superimposed-waves-out-of-phase.png&quot;
/&gt;&lt;/div&gt;
&lt;p&gt;
The amplitude is exactly 0 at t = 5 seconds and it increases gradually
till t = 10 seconds. At t = 10 seconds, the amplitude becomes 0.8 again
and then starts decreasing till t = 15 seconds when it becomes 0 again.
This cycle continues. This can be seen in the graph of the superimposed
waves given below.
&lt;/p&gt;&lt;p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img
src=&quot;http://3.bp.blogspot.com/_CihYmKJVITc/TUngWoWVNKI/AAAAAAAAA5M/zLQTybaKdms/superimposed-waves.png&quot;
/&gt;&lt;/div&gt;
&lt;p&gt;
Let us understand this mathematically. Let us represent both the sine
waves with the functions: y&lt;sub&gt;1&lt;/sub&gt; = A sin 2&amp;pi;ft and
y&lt;sub&gt;2&lt;/sub&gt;  = A sin 2&amp;pi;(f + &amp;Delta;f)t, where A is the amplitude of
the waves, and f and f + &amp;Delta;f are the frequency of the two waves.
&lt;/p&gt;&lt;p&gt;
So, the superimposed waves can be represented as
&lt;/p&gt;&lt;p&gt;
y&lt;sub&gt;1&lt;/sub&gt; + y&lt;sub&gt;2&lt;/sub&gt;
&lt;/p&gt;&lt;p&gt;
= A sin 2&amp;pi;ft + A sin 2&amp;pi;(f + &amp;Delta;f)t
&lt;/p&gt;&lt;p&gt;
= (2A cos 2&amp;pi; &amp;middot; &lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt; &amp;middot;
t) sin 2&amp;pi;(f + &lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt;)t
&lt;/p&gt;&lt;p&gt;
To understand this function intuitively, imagine it as a sine wave with
a frequency of f + &lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt; and an
amplitude of
2A&amp;nbsp;cos&amp;nbsp;2&amp;pi;&amp;nbsp;&amp;middot;&amp;nbsp;&lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt;&amp;nbsp;&amp;middot;&amp;nbsp;t.
In our case, f + &lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt; = 500.5 Hz. So,
the superimposed waves look like a sine wave with a frequency of 500.5
Hz but with its amplitude varying with time at a frequency of
&lt;sup&gt;&amp;Delta;f&lt;/sup&gt;&amp;frasl;&lt;sub&gt;2&lt;/sub&gt;. In other words, the amplitude
oscillates between 0 and 2A every &lt;sup&gt;1&lt;/sup&gt;&amp;frasl;&lt;sub&gt;&amp;Delta;f&lt;/sub&gt;
seconds. Since &amp;Delta;f = 0.1 Hz and A = 0.4 in our case, we see that
the amplitude of the 500.5 Hz sine wave oscillates between 0 and 0.8
every 10 seconds.
&lt;/p&gt;        </description>
        <pubDate>Thu, 03 Feb 2011 01:32:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/listening-to-superimposed-waves/</guid>
    </item>
    <!-- #flowers - Flowers -->
    <item>
        <title>Flowers</title>
        <link>http://susam.in/blog/flowers/</link>
        <description>
            &lt;p&gt;
&lt;a href=&quot;http://www.youtube.com/watch?v=eOI6Jwx21eE&quot;&gt;Flowers&lt;/a&gt; is my
third attempt at composing music. I composed, played and recorded it
last year but I scribbled it down and prepared the sheet music today.
Hence, this blog post today.
&lt;/p&gt;&lt;p&gt;
The links to the audio files, sheet music, etc. are provided below. The
files reside in my website. In case, my website is down, the YouTube
link provided below should still work.
&lt;/p&gt;&lt;p&gt;
This piece consists of 161 measures. It is approximately 3 minutes 8
seconds long.
&lt;/p&gt;&lt;p&gt;
&lt;div style=&quot;text-align: center&quot;&gt;&lt;audio tabindex=&quot;0&quot;
controls=&quot;controls&quot;&gt;&lt;source
src=&quot;http://susam.in/downloads/music/flowers/flowers.mp3&quot;
type=&quot;audio/mpeg&quot;&gt;&lt;source
src=&quot;http://susam.in/downloads/music/flowers/flowers.ogg&quot;
type=&quot;audio/ogg&quot;&gt;&lt;embed
src=&quot;http://susam.in/downloads/music/flowers/flowers.mp3&quot;
type=&quot;audio/mpeg&quot; autostart=&quot;false&quot; loop=&quot;false&quot;
height=&quot;45&quot;&gt;&lt;/embed&gt;&lt;/audio&gt;
&lt;br&gt;
&lt;a href=&quot;http://susam.in/downloads/music/flowers/flowers.mp3&quot;&gt;&lt;img
style=&quot;position: relative; top: 3px;&quot; title=&quot;Download audio in MP3
format&quot; src=&quot;http://susam.in/images/audio.png&quot;&gt;&lt;/a&gt; &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.mp3&quot;
title=&quot;Download audio in MP3 format&quot;&gt;MP3&lt;/a&gt; | &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.ogg&quot;&gt;&lt;img
style=&quot;position: relative; top: 3px;&quot; title=&quot;Download audio in OGG
Vorbis format&quot; src=&quot;http://susam.in/images/audio.png&quot;&gt;&lt;/a&gt; &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.ogg&quot;
title=&quot;Download audio in OGG Vorbis format&quot;&gt;OGG&lt;/a&gt; | &lt;a
href=&quot;http://www.youtube.com/watch?v=YdXeE0xJnJM&quot;&gt;&lt;img style=&quot;position:
relative; top: 2px;&quot; title=&quot;Listen to 'Flowers' on YouTube&quot;
src=&quot;http://susam.in/images/youtube.png&quot;&gt;&lt;/a&gt; &lt;a
href=&quot;http://www.youtube.com/watch?v=eOI6Jwx21eE&quot; title=&quot;Listen to
'Flowers' on YouTube&quot;&gt;YouTube&lt;/a&gt; | &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.pdf&quot;&gt;&lt;img
style=&quot;position: relative; top: 5px;&quot; title=&quot;PDF sheet music for
'Flowers'&quot; src=&quot;http://susam.in/images/gclef.png&quot; height=&quot;22px&quot;&gt;&lt;/a&gt; &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.pdf&quot; title=&quot;PDF
sheet music for 'Flowers'&quot;&gt;Score&lt;/a&gt; | &lt;a
href=&quot;http://susam.in/downloads/music/flowers/flowers.ly&quot;&gt;&lt;img
style=&quot;position: relative; top: 5px;&quot; title=&quot;LilyPond source for PDF
sheet music&quot; src=&quot;http://susam.in/images/lilypond.gif&quot; width=&quot;22px&quot;&gt;&lt;/a&gt;
&lt;a href=&quot;http://susam.in/downloads/music/flowers/flowers.ly&quot;
title=&quot;LilyPond source for PDF sheet music&quot;&gt;LilyPond&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;
This piece of music embodies love, peace and joy. After I composed the
first few notes and played it, someone told me that the notes bring an
image of flowers in the mind. Most of the music emerged naturally from
my mind. Some of the bass notes did not come out naturally and I decided
them, quite artificially, to fill the music with a pleasant and peaceful
mood. It took me a couple of days to compose this. I played and recorded
this after a day of practice.
&lt;/p&gt;        </description>
        <pubDate>Sat, 22 Jan 2011 23:48:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/flowers/</guid>
    </item>
    <!-- #hidden-form-and-fermats-last-theorem - A hidden form and Fermat's Last Theorem -->
    <item>
        <title>A hidden form and Fermat's Last Theorem</title>
        <link>http://susam.in/blog/hidden-form-and-fermats-last-theorem/</link>
        <description>
            &lt;p&gt;
Here is a problem I made one night for my friends who love to solve
mathematical puzzles.
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
Find all integer solutions for the equation: y&lt;sup&gt;2&lt;/sup&gt; + 3 =
&lt;sup&gt;x&lt;sup&gt;3&lt;/sup&gt;&lt;/sup&gt;&amp;frasl;&lt;sub&gt;18&lt;/sub&gt;.&lt;/div&gt;
&lt;p&gt;
You might want to stop here and think for a while if you want to solve
this yourself. There are spoilers ahead.
&lt;/p&gt;
&lt;p&gt;
In this problem, I have hidden a Diophantine equation of the form
a&lt;sup&gt;n&lt;/sup&gt; + b&lt;sup&gt;n&lt;/sup&gt; = c&lt;sup&gt;n&lt;/sup&gt;. The first clue to this
hidden form might be the equation with its terms rearranged.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
x&lt;sup&gt;3&lt;/sup&gt; = 18y&lt;sup&gt;2&lt;/sup&gt; + 54
&lt;/p&gt;&lt;p&gt;
The right hand side is  2(9y&lt;sup&gt;2&lt;/sup&gt; + 3&lt;sup&gt;3&lt;/sup&gt;). This is of
the form 2(3a&lt;sup&gt;2&lt;/sup&gt;b + b&lt;sup&gt;3&lt;/sup&gt;) which is equal to (a +
b)&lt;sup&gt;3&lt;/sup&gt; &amp;minus; (a &amp;minus; b)&lt;sup&gt;3&lt;/sup&gt;. Now, the hidden form
must be clear. So, let me write down the solution to this problem
neatly.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
y&lt;sup&gt;2&lt;/sup&gt; + 3 = &lt;sup&gt;x&lt;sup&gt;3&lt;/sup&gt;&lt;/sup&gt;&amp;frasl;&lt;sub&gt;18&lt;/sub&gt;&lt;br&gt;
&amp;rArr; x&lt;sup&gt;3&lt;/sup&gt; = 18y&lt;sup&gt;2&lt;/sup&gt; + 54&lt;br&gt;
&amp;rArr; x&lt;sup&gt;3&lt;/sup&gt; = (y + 3)&lt;sup&gt;3&lt;/sup&gt; &amp;minus; (y &amp;minus;
3)&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;
&amp;rArr; x&lt;sup&gt;3&lt;/sup&gt; + (y &amp;minus; 3)&lt;sup&gt;3&lt;/sup&gt; = (y + 3)&lt;sup&gt;3&lt;/sup&gt;
&lt;/p&gt;&lt;p&gt;
From Fermat's last theorem, we know that an equation of the form,
a&lt;sup&gt;n&lt;/sup&gt; + b&lt;sup&gt;n&lt;/sup&gt; = c&lt;sup&gt;n&lt;/sup&gt; has got a trivial solution
a = b = 0 and no other solutions for positive integers a, b and c when n
&amp;gt; 2. Now, let us examine x&lt;sup&gt;3&lt;/sup&gt; + (y &amp;minus; 3)&lt;sup&gt;3&lt;/sup&gt; =
(y + 3)&lt;sup&gt;3&lt;/sup&gt; with the help of Fermat's Last Theorem. A trivial
solution in this case is ruled out because there is no value of y for
which y &amp;minus; 3 = y + 3 = 0.
&lt;/p&gt;&lt;p&gt;
However, if we consider y &amp;minus; 3 = 0, or y = 3, we get an equation
that is no longer of the form required by Fermat's Last Theorem.
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
x&lt;sup&gt;3&lt;/sup&gt; = 6&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;
&amp;rArr; x = 6
&lt;/p&gt;&lt;p&gt;
If we consider y + 3 = 0, or y = &amp;minus;3, again we get
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
x&lt;sup&gt;3&lt;/sup&gt; + (&amp;minus;6)&lt;sup&gt;3&lt;/sup&gt; = 0&lt;br&gt;
&amp;rArr; x = 6.
&lt;/p&gt;&lt;p&gt;
Hence, the integral solutions to the equation are:
&lt;/p&gt;&lt;p class=&quot;math&quot;&gt;
x = 6&lt;br&gt;
y = &amp;plusmn;3
&lt;/p&gt;        </description>
        <pubDate>Wed, 12 Jan 2011 10:12:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/hidden-form-and-fermats-last-theorem/</guid>
    </item>
    <!-- #squaring-numbers-with-5-at-ends - Squaring numbers with 5 at ends -->
    <item>
        <title>Squaring numbers with 5 at ends</title>
        <link>http://susam.in/blog/squaring-numbers-with-5-at-ends/</link>
        <description>
            &lt;p&gt;
In this post, I'll discuss some simple tricks I use to square numbers
that begin or end with the digit 5. Let me illustrate each trick with
two examples for 2-digit numbers. Later, in this post, I'll generalize
the trick for n-digit numbers where n &amp;ge; 2 after explaining why the
tricks work using algebra.
&lt;/p&gt;&lt;p&gt;
&lt;span class=&quot;leader&quot;&gt;Squaring a 2-digit number that ends with 5&lt;/span&gt;
&lt;/p&gt;&lt;p&gt;
I learnt this from a book when I was in 5th standard. For a 2-digit
number that ends with 5, let us assume that the first digit is A. So,
the number can be written as A5. The square is: A &amp;times; (A + 1)
followed by 25, i.e. the first few digits of the square is given by A
&amp;times; (A + 1) and the last two digits are 25. Let us try some
examples.
&lt;/p&gt;&lt;p&gt;
25&lt;sup&gt;2&lt;/sup&gt; = 625. (2 &amp;times; 3 = 6.)
85&lt;sup&gt;2&lt;/sup&gt; = 7225. (8 &amp;times; 9 = 72.)
&lt;/p&gt;&lt;p&gt;
&lt;span class=&quot;leader&quot;&gt;Squaring a 2-digit number that begins with 5&lt;/span&gt;
&lt;/p&gt;&lt;p&gt;
After learning that trick from the book, I wondered if I could make more
tricks for myself. This is the first one I could come up with. If the
first digit of a 2-digit number is 5 and the second digit is A, i.e. if
the number is of the form 5A, the square is given by the sum of 25 and A
followed by square of A. In other words, the first two digits of the
square are obtained from 25 + A and the last two digits are obtained
from A&lt;sup&gt;2&lt;/sup&gt;. Let us try some examples.
&lt;/p&gt;&lt;p&gt;
52&lt;sup&gt;2&lt;/sup&gt; = 2704. (25 + 2 = 27 and 2&lt;sup&gt;2&lt;/sup&gt; = 4.)
57&lt;sup&gt;2&lt;/sup&gt; = 3249. (25 + 7 = 32 and 7&lt;sup&gt;2&lt;/sup&gt; = 49.)
&lt;/p&gt;&lt;p&gt;
&lt;span class=&quot;leader&quot;&gt;Squaring an n-digit number that ends with 5&lt;/span&gt;
&lt;/p&gt;&lt;p&gt;
Let us represent all digits except the last one as A. So, the number is
of the form A5 where A represents one or more digits. e.g. if the number
is 115, A = 11.
&lt;/p&gt;&lt;p&gt;
So, we can say that the number of the form A5 can be algebraically
expressed as 10A + 5. Now, (10A + 5)&lt;sup&gt;2&lt;/sup&gt; = 100A&lt;sup&gt;2&lt;/sup&gt; +
100A + 25 = 100A(A + 1) + 25. This amounts to writing A(A + 1) followed
by 25.
&lt;/p&gt;&lt;p&gt;
Let us try some examples:
&lt;/p&gt;&lt;p&gt;
115&lt;sup&gt;2&lt;/sup&gt; = 13225. (11 &amp;times; 12 = 132.)
9995&lt;sup&gt;2&lt;/sup&gt; = 99900025. (999 &amp;times; 1000 = 999000.)
&lt;/p&gt;&lt;p&gt;
&lt;span class=&quot;leader&quot;&gt;Squaring an n-digit number that begins with
5&lt;/span&gt;.
&lt;/p&gt;&lt;p&gt;
Let us represent all digits except the first one as A. So the number is
of the form 5A where A represents one or more digits. e.g. if the number
is 512, A = 12.
&lt;/p&gt;&lt;p&gt;
So, we can say that the number of the form 5A can be algebraically
expressed as 5 &amp;times 10&lt;sup&gt;n&lt;/sup&gt; + A where n is the number of digits
in A, i.e. the number of digits after the first digit which is 5. Now,
(5 &amp;times 10&lt;sup&gt;n&lt;/sup&gt; + A)&lt;sup&gt;2&lt;/sup&gt; = 25 &amp;times 10&lt;sup&gt;2n&lt;/sup&gt; +
10&lt;sup&gt;n + 1&lt;/sup&gt;A + A&lt;sup&gt;2&lt;/sup&gt;. This amounts to writing 25 as the
first two digits followed by A&lt;sup&gt;2&lt;/sup&gt; as the last 2n digits and
then adding A to it by placing it just below the second digit which is
5. Some examples follow.
&lt;/p&gt;&lt;p&gt;
502&lt;sup&gt;2&lt;/sup&gt;
&lt;pre style=&quot;color: black; margin: 0&quot;&gt;= 250004   (2&lt;sup&gt;2&lt;/sup&gt; = 4.)
  +02      (02 placed under 5.)
---------
  252004
---------
&lt;/pre&gt;
512&lt;sup&gt;2&lt;/sup&gt;
&lt;pre style=&quot;color: black; margin: 0&quot;&gt;= 250144   (12&lt;sup&gt;2&lt;/sup&gt; = 144.)
  +12      (12 placed under 5.)
---------
  262144
---------
&lt;/pre&gt;

&lt;span class=&quot;leader&quot;&gt;Using both tricks together&lt;/span&gt;
&lt;/p&gt;&lt;p&gt;
5195&lt;sup&gt;2&lt;/sup&gt;
&lt;pre style=&quot;color: black; margin: 0&quot;&gt;= 25038025 (195&lt;sup&gt;2&lt;/sup&gt; =
38025; 19 &amp;times; 20 = 380.)
  +195     (195 placed under 5.)
-----------
  26988025 
-----------
&lt;/pre&gt;

Note that in the problem above, we use the second trick to square 5195
because it begins with the digit 5. However, while doing so, we need to
figure the square of 195 and we use the first trick to do this.
&lt;/p&gt;        </description>
        <pubDate>Tue, 21 Dec 2010 05:03:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/squaring-numbers-with-5-at-ends/</guid>
    </item>
    <!-- #pi-pm - &amp;pi; PM -->
    <item>
        <title>&amp;pi; PM</title>
        <link>http://susam.in/blog/pi-pm/</link>
        <description>
            &lt;p&gt;
For so long, I have been wishing &quot;Happy Pi PM&quot; to my friends and
colleagues at 3:14 PM. However, this doesn't seem to be appropriate
since 3:14 PM is not really 3.14 PM. In fact, 3:14 PM is
3&lt;sup&gt;14&lt;/sup&gt;&amp;frasl;&lt;sub&gt;60&lt;/sub&gt; PM which when expressed in decimal
representation turns out to be 3.23333 PM, accurate up to 5 decimal
places.
&lt;/p&gt;&lt;p&gt;
The value of &amp;pi; accurate up to 5 decimal places is 3.14159. Now,
.14159 hour = 509.724 seconds &amp;asymp; 8 minutes and 30 seconds, or
8&amp;frac12; minutes.
&lt;/p&gt;&lt;p&gt;
So, &amp;pi; PM turns out be 3:08:30 PM or 8&amp;frac12; minutes past 3 PM.
&lt;/p&gt;        </description>
        <pubDate>Mon, 20 Dec 2010 09:38:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/pi-pm/</guid>
    </item>
    <!-- #belated-birthday - Belated birthday -->
    <item>
        <title>Belated birthday</title>
        <link>http://susam.in/blog/belated-birthday/</link>
        <description>
            &lt;p&gt;
This is a poem I wrote for a friend when I was in school. The last two
lines are inspired from an Archies card.
&lt;/p&gt;&lt;p&gt;
BELATED BIRTHDAY
&lt;/p&gt;&lt;p&gt;
All days come but once in annum,&lt;br&gt;
Amongst them all very special are few,&lt;br&gt;
Amongst the few you love the most is one,&lt;br&gt;
The day when the world got you.&lt;br&gt;
&lt;/p&gt;&lt;p&gt;
You celebrate the day you're born&lt;br&gt;
And want your chums to be near.&lt;br&gt;
I regret that I left you lorn,&lt;br&gt;
Hope you pardon me, O dear!&lt;br&gt;
&lt;/p&gt;&lt;p&gt;
Shamelessly I wish you, 'A belated very happy birthday'.&lt;br&gt;
Though I know, now it is very late;&lt;br&gt;
But I swear, I didn't forget your birthday&lt;br&gt;
I just forgot the date.&lt;br&gt;
&lt;/p&gt;        </description>
        <pubDate>Wed, 01 Dec 2010 05:09:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/belated-birthday/</guid>
    </item>
    <!-- #clumsy-pointers - Clumsy pointers -->
    <item>
        <title>Clumsy pointers</title>
        <link>http://susam.in/blog/clumsy-pointers/</link>
        <description>
            &lt;p&gt;
Here is a silly puzzle I solved exactly one month ago.
&lt;div class=&quot;problem&quot;&gt;
Without using &lt;code&gt;typedef&lt;/code&gt;, declare a pointer to a function
which has an array of 10 pointers to functions which have int * argument
and return type void as argument, and returns a pointer to a function
which has int * argument and return type void.&lt;/div&gt;
&lt;p&gt;
If the above problem statement is difficult to understand, here is the
same problem expressed in terms of &lt;code&gt;typedef&lt;/code&gt;.
&lt;/p&gt;
&lt;div class=&quot;problem&quot;&gt;
Without using &lt;code&gt;typedef&lt;/code&gt;, declare a pointer that is equivalent
to the following declaration of &lt;code&gt;x&lt;/code&gt;.
&lt;/p&gt;&lt;pre&gt;
typedef void (*func_t)(int *);
func_t (*x)(func_t [10]);
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I'll describe how I solve such problems. I start from the right end of
the problem and work my way to the left most end defining each part one
by one.
&lt;/p&gt;&lt;p&gt;
Let me start.
&lt;div style=&quot;text-align: center&quot;&gt;&lt;b&gt;&lt;code&gt;void x(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
A function which has int * argument and return type void.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;void (*x)(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
Pointer to a function which has int * argument and return type void.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;void (*x())(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
A function that returns a pointer to a function which has int * argument
and return type void.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;void (*x(void (*)(int *)))(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
A function which has a pointer to a function that has int * argument and
return type void as argument, and returns a pointer to a function which
has int * argument and return type void.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;void (*x(void (*[10])(int *)))(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
A function which has an array of 10 pointers to functions that has int *
argument and return type void as argument, and returns a pointer to a
function which has int * argument and return type void.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;void (*(*x)(void (*[10])(int *)))(int *)&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
A pointer to a function which has an array of 10 pointers to functions
that has int * argument and return type void as argument, and returns a
pointer to a function which has int * argument and return type void.
&lt;/div&gt;
&lt;p&gt;
I verified the declaration with a short code.
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
#include &amp;lt;stdio.h&amp;gt;

/* A function which has int * argument and return type void. */
void g(int *a)
{
    printf(&quot;g(): a = %d\n&quot;, *a);
}

/* A function which has an array of 10 pointers to functions that has
 * int * argument and return type void as argument, and returns a
 * pointer to a function which has int * argument and return type void.
 */
void (*f(void (*a[10])(int *)))(int *)
{
    int i;
    for (i = 0; i &amp;lt; 10; i++)
        a[i](&amp;amp;i);
    return g;
}

int main()
{
    /* An array of 10 pointers to functions that has int * argument and
     * return type void. */
    void (*a[10])(int *) = {g, g, g, g, g, g, g, g, g, g};

    /* A pointer to the function which has an array of 10 pointers to
     * functions that has int * argument and return type void as
     * argument, and returns a pointer to a function which has int *
     * argument and return type void. */
    void (*(*x)(void (*[10])(int *)))(int *) = f;

    /* A pointer to a function that has int * argument and return type
     * void. */
    void (*y)(int *a) = x(a);

    int i = 10;
    y(&amp;amp;i);
    return 0;
}
&lt;/pre&gt;
&lt;p&gt;
Section 5.12 (Complicated Declarations) of the second edition of &lt;a
class=&quot;title&quot;
href=&quot;http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628&quot;&gt;The
C Programming Language&lt;/a&gt; by &lt;span class=&quot;author&quot;&gt;Brian W.
Kernighan&lt;/span&gt; and &lt;span class=&quot;author&quot;&gt;Dennis M. Ritchie&lt;/span&gt; has
some good examples of complicated declarations of pointers although they
are less clumsy than the one in this blog post. Let me mention a couple
of interesting ones from that section.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;char (*(*x())[])()&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
x: function returning pointer to array[] of pointer to function
returning char.
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;&lt;code&gt;char (*(*x[3])())[5]&lt;/code&gt;&lt;/b&gt;&lt;br&gt;
x: array[3] of pointer to function returning pointer to array[5] of
char.
&lt;/p&gt;        </description>
        <pubDate>Mon, 29 Nov 2010 14:27:00 +0000</pubDate>
        <dc:creator>Susam Pal</dc:creator>
        <guid>http://susam.in/blog/clumsy-pointers/</guid>
    </item>
</channel>
</rss>

