Friday, July 23, 2010

n-odd-digit number divisible by 5n

Solution to a problem asked by Rohan at RSA.
Prove that for every positive integer n there exists an n-digit number divisible by 5n all of whose digits are odd.
Before we proceed let us keep a note of a little thing we'll need.

1 ≡ -4 (mod 5)
3 ≡ -2 (mod 5)
5 ≡ 0 (mod 5)
7 ≡ -3 (mod 5)
9 ≡ -1 (mod 5)

So, the set {1, 3, 5, 7, 9} covers all possible values in mod 5 arithmetic. Now, let us begin the proof.

The statement is clearly true for n = 1 as there exists 5 which is a 1-digit number divisibile by 51 and all its digits are odd.

Let us assume that the statement is true for n = N where N is a positive integer. So, there exists an integer m which is N-digit, divisible by 5N and all its digits are odd.

Now, we will show that for n = N + 1, there exists an integer k10N + m divisible by 5N+1 where k ∈ {1, 3, 5, 7, 9};

k10N + m ≡ 0 (mod 5N+1)

⇔ 5N(k2N + m5N) ≡ 0 (mod 5N+1)

⇔ k2N + m5N ≡ 0 (mod 5)

⇔ k6N + m5N ⋅ 3N ≡ 0 (mod 5)

⇔ k ≡ -m5N ⋅ 3N (mod 5)

⇔ k ∈ {1, 3, 5, 7, 9} … (from the note in the beginning)

So, we have shown that for some odd digit k, we have a number k10N + m divisible by 5N+1. Also, note that this number is an N+1 digit number because m was an N digit number.

Hence, from the principle of mathematical induction, the proof is complete for all positive integers n.

We can use this proof to generate the n-digit numbers for all values of n.

1-digit number: 5
2-digit number: 75 since k = -551 ⋅ 31 mod 5 = -3 mod 5 = 7.
3-digit number: 375 since k = -7552 ⋅ 32 mod 5 = -2 mod 5 = 3.
4-digit number: 9375 since k = -37553 ⋅ 33 mod 5 = -1 mod 5 = 9.
and so on …

OEIS entry for this sequence: A151752

Thursday, July 22, 2010

Combinatorial coincidence worksheets

The result obtained in the problem mentioned in combinatorial coincidence blog post is true for this pseudocode as well.
count = 0
for c1 in 1 to n:
for c2 in c1 to n:
for c3 in c2 to n:
...
...
for ck in ck-1 to n:
count++ 

Experimenting with Faulhaber's formula

The first line for f4(n) incorrectly uses the variable n inside the summation. It should be k instead.

Sticks and balls example

Sunday, June 20, 2010

Big-endian on little-endian

I wanted big-endian emulation on my little-endian Intel machine to test a program for byte order related issues. QEMU PowerPC emulator seemed like a good solution. I have documented the steps to set it up in this note.
  1. Installed QEMU.
    nifty:~# aptitude update && aptitude install qemu
    
  2. Downloaded Mac-on-Linux from http://sourceforge.net/projects/mac-on-linux/files/ and copied the 'video.x' file in the download to '/usr/share/qemu'. This was necessary to prevent qemu-system-ppc from complaining about it.
    nifty:~# tar -xjf mol-0.9.72.1.tar.bz2
    nifty:~# cp mol-0.9.72.1/mollib/drivers/video.x /usr/share/qemu
  3. Downloaded Debian for PowerPC and installed it on a QEMU hard disk image.
    susam@nifty:~/qemu$ wget --no-verbose http://cdimage.debian.org/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso
    2010-06-19 02:55:06 URL:http://caesar.acc.umu.se/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso[675569664/675569664] -> "debian-504-powerpc-CD-1.iso" [1]
    susam@nifty:~/qemu$ qemu-img create powerpc.img 2G
    Formatting 'powerpc.img', fmt=raw size=2147483648
    susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -cdrom debian-504-powerpc-CD-1.iso -boot d -m 512
    
  4. Booted the QEMU PowerPC emulator with the hard disk image.
    susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -m 512
    
  5. Verified that I was really on a big endian system by writing a simple C program.
    susam@lilliput:~$ cat endian.c
    #include <stdio.h>
    
    int main()
    {
        int n = 0x1;
        printf(*((char *) &n) ? "little-endian\n" : "big-endian\n");
        return 0;
    }
    susam@lilliput:~$ gcc endian.c && ./a.out
    big-endian
    susam@lilliput:~$ 
    
In case you missed the pun, Lilliputians were originally big-endians.

An HTML element with ID behaves like a global constant in IE

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Experiments on ID attributes</title>
<script type="text/javascript">
function foo()
{
    try {
        a = 10
        alert(a)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
</script>
</head>
<body onload="foo()">
<p id="a">
hello, world
</p>
</body>
</html>
This code works fine in Firefox 3.6 and Chrome 5.0.

However, the statement a = 10 causes an error in Internet Explorer 6.0.

To confirm my guess that a indeed behaves like a global constant object, I replaced function foo() with this.
function foo()
{
    try {
        alert(a.innerHTML)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
Indeed, in IE, the alert dialog box displays what I expected.

However, on Firefox it is an error:

One way I could fix the code is by defining function foo in this manner
function foo()
{
    try {
        var a = 10
        alert(a)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
Note the var in the declaration.

Realized while writing this note that e.description is not a standard way of getting the error description from the Error object. The description property is defined for Error objects in IE only. The message property is standard and works on IE as well as other browsers.

Friday, June 18, 2010

Laws of correction

McKean's Law
Any correction of the speech or writing of others will contain at least one grammatical, spelling, or typographical error.

Skitt's Law
Any post correcting an error in another post will contain at least one error itself.

Muphry's Law
If you write anything criticizing editing or proofreading, there will be a fault of some kind in what you have written.

Hartman's Law of Prescriptivist Retaliation
Any article or statement about correct grammar, punctuation, or spelling is bound to contain at least one eror.

Sunday, June 13, 2010

Finding the second incoming link to the second node in a loopy linked list

It is already given that the loop is formed at the second node of the linked list, i.e. the second node has two incoming links, one from the first and the other from the last node. The address of the last node that links to the second node and forms the loop needs to be found.
/* Solution to a problem asked at:
   http://www.orkut.co.in/Main#CommMsgs?cmm=1027&tid=5470285126748556338

   Author: Susam Pal
 */

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

/* Detects the loop and returns the address to the last node that
   links to the second node, i.e. head->next. Consumes O(1) memory
   and O(n) time.
 */
struct node *last_node(struct node *head)
{
    struct node *p1;
    struct node *p2;

    p1 = p2 = head->next;
    while (p1->next != p2->next->next) {
        p1 = p1->next;
        p2 = p2->next->next;
    }
    return p1;
}

struct node *create_node(int data)
{
    struct node *p;
    p = (struct node *) malloc(sizeof *p);
    p->data = data;
    return p;
}

int main()
{
    struct node *p;
    struct node *head;
    struct node *second;
    struct node *second_node;

    p = head = create_node(1);
    p = p->next = create_node(2);
    p = p->next = create_node(3);
    p = p->next = create_node(4);
    p = p->next = create_node(5);
    p = p->next = create_node(6);
    p = p->next = create_node(7);
    p->next = head->next; /* Loop to second node */

    p = last_node(head);
    printf("Last node has address %p and data %d.\n", p, p->data);
    return 0;
}
Output from my system:
Last node has address 0x7330d0 and data 7.

JavaScript array length and trailing comma

Found this issue with Internet Explorer long back when I was developing QuickQWERTY.

If a JavaScript array is defined in the following manner
var a = ['a', 'b', 'c',]
a.length evaluates to 3 in most browsers whereas Internet Explorer evaluates it to 4.

From an experiment, I performed in December 2008, I found that the following browsers evaluate the length of the above array to 3.
  1. Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20
  2. Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
  3. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.29 Safari/525.19
  4. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/528.1 (KHTML, like Gecko) Version/4.0 Safari/528.1.1
  5. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.18) Gecko/20081029 Firefox/2.0.0.18
  6. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.1
  7. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
  8. Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
  9. Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
  10. Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.18) Gecko/20081113 Ubuntu/7.10 (gutsy) Firefox/2.0.0.18
  11. Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008101315 Ubuntu/8.10 (intrepid) Firefox/3.0.3
  12. Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.4) Gecko/2008112309 Iceweasel/3.0.4 (Debian-3.0.4-1)
The following browsers evaluate the length of the above array to 4.
  1. Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; MS-RTC LM 8; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
  2. Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; InfoPath.1; .NET CLR 2.0.40607)
  3. Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
  4. Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Zango 10.3.75.0)
  5. Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
  6. Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
  7. Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)
The HTML page I created this for experiment is still available at: http://susam.in/lab/array-length/. I found the URL to the old twitter update in which I asked my twitter contacts for help in this experiment using http://tweetfinder.net/.