Wednesday, December 27, 2006

Program: Print the electronic configuration of an element

I had not written a C program since a long long time and decided to have a go at writing one. I was so low on confidence that I first wrote the "Hello World" program and was deliriously happy when it compiled without any errors. And then fell off my chair with uncontrollable joy when it produced the expected output.

I could have stopped then and there - a happy man. But, I have a tendency to get myself in troubling situations. Somehow, I ended up deciding to write a program to print the electronic configuration of an element given its atomic number. After fighting ferociously with logical and syntactic errors, I managed to complete the program. However, I did not have the patience to handle the exceptions in 3d,4d,5d.

For example, if you enter the atomic number as 15(phosphorus), then the program prints out:
1s2 2s2 2p6 3s2 3p3

Someday, if i gather enough patience, I will modify the program to handle these exceptions. Until then, just look upon the program as a mental exercise.


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

#define AMIN 1
#define AMAX 120

int get_sub_shell_name(int snumber)
{
if(snumber<1) {
printf(
"get_sub_shell_name: Invalid shell number %d", snumber);
return -1;
}
switch(snumber) {
case 1: return 's';
case 2: return 'p';
case 3: return 'd';
case 4: return 'f';
default: return 'f'+(snumber-4);
}
}

int get_max_electrons(int n)
{
int total = 2;
int i;
for(i=1;i<n;i++) {
total+=
4;
}
return total;
}

/* returns the number of electrons yet to be accounted for */
int print_next_diagonal(int rem, int row, int col)
{
int i = row;
int j = col;
int snumber, sname, enumber;

while(j>0 && rem>0) {
snumber = i;
sname = get_sub_shell_name(j);
enumber = get_max_electrons(j);
if(rem < enumber)
enumber = rem;

printf(
"%d", snumber);
printf(
"%c", sname);
printf(
"%d ", enumber);

rem -= enumber;
i++;
j--;
}
return rem;
}


void print_shell_configuration(int anumber)
{
int i, j, rem, ret;

i = j =
1;
rem = anumber;
while(rem > 0) {
while(j <= i && rem > 0) {
rem = print_next_diagonal(rem, i, j);
j++;
}
i++;
j = i -
1;
}
}


int main()
{
int anumber;

printf(
"Enter the atomic number of the element:");
scanf(
"%d", &anumber);

if(anumber < AMIN) {
printf(
"Atomic number cannot be zero or negative.\n");
exit(
1);
}

if(anumber > AMAX) {
printf(
"The element does not exist. Perhaps you have found a new one.\n");
exit(
1);
}

print_shell_configuration(anumber);
printf(
"\n");

return 0;
}


Monday, June 12, 2006

Online chess

http://www.redhotpawn.com/

Petrol additives

A good page explaining why lead was added to petrol in the first place, what engine knocking is, and why unleaded petrol should be used.

http://www.chm.bris.ac.uk/motm/leadtet/leadh.htm


And a good page explaining engine knocking.

http://www.stanford.edu/~bmoses/knock.html


And the ultimate gasoline faq.

http://www.repairfaq.org/filipg/AUTO/F_Gasoline.html

Wednesday, June 07, 2006

Nice online game (notpron)

http://deathball.net/notpron/

Tuesday, May 30, 2006

Remembering Ohm's law

Came across this beautiful way of remembering Ohm's law V = I*R.

Arrange the letters in the form of a triangle as follows. It is as if the area of the triangle has been divided into 3 areas each represented by V, I and R.

V
- - -
I | R

* To obtain the formula for I, just cross out I from the figure and you are left with V/R.
* To obtain the formula for R, just cross out R from the figure and you are left with V/I.
* To obtain the formula for V, just cross out V from the figure and you are left with IR.

Nothing special really, but I think it's beautiful !:)

Infact, this arrangement could be used to represent any equation of the form A=B*C.

Tuesday, May 23, 2006

Josephus Problem

I decided to give this one a try. I knew that there exists an algorithm to solve this problem but it is more fun to try, fail, then try once more, fail again and then pounce on the known algorithm when the suspense becomes unbearable !:D

The simplest idea that comes to mind is to create a circular singly-linked list from the numbers and go on deleting the nodes, which is quite a violent activity actually, since it is equivalent to killing the people in the circle !!:D. So the next time you decide to delete a node, think twice :P

Anyways, this algorithm though quite simple is probably overkill for the problem and involves linked list manipulations. Equivalently, an array may be used as well, where the killed soldiers are marked by a special entry in the array.

Then, I had a glance at the algorithm and noticed that it doesnt use arrays or linked lists. I decided to give this approach a try.

But the question was what to track ? Do I track the last number that was deleted or the last number remaining after a round of deleting numbers from the array ?

(more coming soon ... )

Monday, May 22, 2006

Card Trick - Declaring the Nth card

Split a deck of cards into two halves, say H1 and H2. Select any one half, say H2 and lay down all the cards face up one-by-one. Then pick them up without disturbing the original order and place them below H1.

Now, hold the deck so that the cards are facing down.
Draw the first card from the deck and place it face up. If it is any one of K,Q or J, consider it equivalent to a 10. The idea is as follows:

Let the card be 'x'. Now count from 'x' upto 10 and for each number in this count, draw a card from the deck and place it face down on 'x'.

Repeat the above procedure for two more cards, say 'y' and 'z'.
Now, let N = x + y + z.

Say that the Nth card in the remaining deck of cards in ur hand is so-and-so and then hey presto ! The Nth card is indeed what you said it would be.

Trick:
------
Initially, when you are the putting down the cards from one of the halves, remember the 7th card.

Logic:
------
Let the cards that are put down be x, y and z. Consider a card x. You draw cards from the deck and place them on x until you reach 10. That is, if the card is x, you draw (11-x) cards from the deck including x.

So initially, you draw (11-x + 11-y + 11-z) cards from the deck. Then, you draw N more cards from the deck where N = x + y + z.

Hence, in toto, you draw ( 11-x + 11-y + 11-z + x + y + z ) = 33 cards from the deck.

If you know the 33th card, you are done ! That is why, initially you lay down one of the halves and remember the 7th card and then place this half below the other.

So go ahead and take the second step in becoming a magician. The first one is ofcourse growing whiskers and acting strange and mysterious about anything and everything !!:D

Monday, May 15, 2006

Non-recursive tree traversals

Was asked recently by a friend to try this out. They are all almost the same except that postorder is a bit tricky. And do drop a gentle and kind comment if there are any mistakes :D

So here goes ...


Inorder:
--------
inorder(node *ptr)
{
lptr = ptr;
while(lptr!=NULL) {
stack.push(lptr);
lptr = lptr->left;
}

while(!stack.empty()) {
p = stack.pop();
print(p->data);

q = p->right;
while(q!=NULL) {
stack.push(q);
q = q->left;
}
}
}


Preorder:
---------
preorder(node *ptr)
{
lptr = ptr;
while(lptr!=NULL) {
print(lptr->data);
stack.push(lptr);
lptr = lptr->left;
}

while(!stack.empty()) {
p = stack.pop();
q = p->right;
while(q!=NULL) {
print(q->data);
stack.push(q);
q = q->left;
}
}
}


Postorder:
----------
postorder(node *ptr)
{
lptr = ptr;
while(lptr!=NULL) {
stack.push(lptr);
lptr = lptr->left;
}

while(!stack.empty()) {
p = stack.pop();

if(p->right==NULL || p->right==last) {
print(p->data);
}
else {
q = p->right;

// if right child exists, push back the parent node onto the stack
if(q!=NULL)
stack.push(p);

while(q!=NULL) {
stack.push(q);
q = q->left;
}
}
last = p;
}
}

Fibonacci numbers

I came across three algorithms to compute the nth Fibonacci number. The last one is beautiful :)

(1) Recursive algorithm (exponential time)
(2) Non-recursive algorithm (linear time)
(3) Algorithm based on the usage of matrices (logarithmic time)

For details, refer http://en.wikipedia.org/wiki/Fibonacci_number

Sunday, May 07, 2006

First post

Hello world !!