Friday, October 15, 2004

Magic Triangle java program

Problem: Compute a magic triangle with 4 nodes to a side. Fill the
nodes with integers from 1 to 10 such that the sum of the nodes on all
three sides is equal. Also, the sum of the node 2-5-9 and 3-5-8 and
4-5-6 are also equal, but they do not need to equal the sides.



// 1
// 2 3
// 4 5 6
// 7 8 9 10

import java.util.*;

public class MagicTriangle {
public static void main(String[] args ){
ArrayList numbers = new ArrayList();
int [] x = new int[10];
for( int i = 0; i < 10; ++ i ) {
numbers.add( new Integer(i + 1));
}

while( true ) {
Collections.shuffle( numbers );
for( int i =0; i < 10; ++i ) {
x[i] = ((Integer)numbers.get(i)).intValue();
}

int magic = x[0] + x[1] + x[3] + x[6];
if( magic == (x[0] + x[2] + x[5]+ x[9]) &&
magic == ( x[6] + x[7] + x[8] + x[9] )) {
int magic2 = x[1] + x[4] + x[8];
if( magic2 == (x[2] + x[4] + x[7] ) &&
magic2 == (x[3] + x[4] + x[5]) ) {
System.out.println("VVVVVV " + magic + " VVVVVVVVVVV");
System.out.println("\t\t\t" + x[0]);
System.out.println("\t\t" + x[1] + "\t\t" + x[2]);
System.out.println("\t" + x[3] + "\t\t" + x[4]+ "\t\t" + x[5]);
System.out.println(x[6] + "\t\t" + x[7]+ "\t\t" + x[8]+"\t\t" + x[9]);
System.out.println("^^^^^^^^^^^^^^^^^^^^");
}

}
}
}
}


Thursday, October 14, 2004

The Prince and the Peasant

During the dark morning hours before a battle, only a few men can sleep. Dawn will bring a call to battle to defend both king and country against their enemies. During these dark hours of quiet, some watch the east, urging the dawn to arrive. While others wish the night would last forever.

The Prince

The army needs a leader. As the royal son of a noble king the prince leads his men with confidence and courage. His sword is the finest in all the land, handed down from royal father to noble son for 6 generations, since King Leroy I. Never has an army whose commander wields this sword ever lost in battle. The commander spends the last few hours before battle encouraging his men.

The Knights

First he visits the knights. Sitting around a huge bonfire are all the knights of the kingdom. As well armed, well trained battle hardened veterans they are ready for a good fight. They spend the time before battle sharpening their swords, mocking the enemy and boasting of their exploits. The prince joins them, patting a couple on the back as a brother in arms. He listens and laughs. One of the knights stands and predicts, "Those cowards will take one look at me and run all the way back to their sorry, smelly, pig-sty houses". The prince laughs and says, "Yes, but only if they can see your ugly face under your helmet". After a few more jests, the prince leaves to encourage the rest of his men. The knights continue boasting and jesting until by sunrise, each of them could single-handedly defeat the enemy; just by the might of his own two hands.

The prince changes out of his princely clothes and into those wore by a peasant. The vast majority of his army are not knights, but peasants. These simple farmers have been pressed into service. By dressing as a peasant, the royal son of a noble king can better encourage his men, as a brother in arms.

The Peasant

All around the huge bonfire of the knights are small fires dotting the night with points of light. Around each fire is a group of peasant-soldiers. They don't have the shiny armor or weapons. Their armor is whatever they could find to wear. And their weapons are mostly farm implements: pitchforks, scythes, axes, staves.

The prince approaches one small fire where his men sit silently, staring blankly into the flames. One holds a sword in his hands. It is not a bright shiny sword like princes. Instead, it is rusty in places and has a few nicks in it.

The prince approaches and asks "Is there room for one more?". No one answers. The one with the sword motions to the log next to him and continues to stare into the fire. The prince sits down and begins staring into the fire. After a few minutes he looks at the said and says, "Tell me, has that sword been in your family long?"

A bit surprised, the peasant takes some time to answer. Finally without looking away from the flames, he says, "six generations". After a long pause he continues, "William, my grandfather's grandfather's father fought several times with King Leo I. At the end of one battle, he found this sword on the battle field after the enemy had been defeated. Since that time each generation of my family has brought this sword into the service of each of the kings since Leo I."

The prince replied, "Its a fine weapon. Looks to have seen quite a bit of action".

"It has, take this gouge here near the hilt. I will bet you a turnip on Tuesday you can't tell where that came from. My grandfather's father put that in when he struck an actual knight with it. He told how all of a sudden he saw the knight, so he swung as hard as he could and caught him right in the helmet. Of course, it didn't hurt the knight none but it did make that there notch in his sword. He also did say it left a fine dent it knight's helmet as well. And he lived to tell the tale as well."

The peasant continued, "I'll bet you did not know that when one of my family brings this sword into battle, the enemy is always defeated. It has never once failed to work."

The prince replied, "With our royal prince to lead us and your noble sword to fight for us, we can't help win to day."

The prince then moved on to other small fires while the peasant tried to remove some of the rust off his noble sword.

The Battle

Dawn! Horns sounds! Drums beat! The time for waiting is over. Each army is lined up and ready for battle. The knights and prince, impressive in their armor and weapons, are mounted on horses and stand ready. All around the rest of the army is on foot with whatever weapon they brought. Then, all is quiet for a moment. Each leader is waiting for the right moment. Then, as if they could read each other's mind, both the prince and the opposing commander decide that now is the right moment. The prince draws his sword out of the scabbard, stands up in the stirrups and shouts, "For King and For Country!" With horses charging and men running to meet the enemy, the battle is begun.

The peasant swordsman finds himself on the edge of the fray. The battle has left him behind. As his sits, he sees the prince being chased by the enemy, surrounding him and his brave knights. Fearing the battle lost, the peasant throws down his sword, breaking the blade. Then, on his hands and knees, the coward crawls off the battlefield.

The Opportunity

The prince's army is dispersed. His royal sword is broken and he is running for his life. Just as he is about to be caught, he finds a broken sword, with a gouge in the blade near the hilt. He grabs it and is able to slay his pursuer. Then with the cry of "For King and For Country", he is able to rally his men and defeat the enemy.

Moral

Cowardice throws opportunity away while courage capitalizes on the missed opportunities of others.

Circus Time

When a think of a circus I see peanuts, elephants, clowns, lions,
motorcycles, horses, popcorn, high-wire and sawdust and then I smell all
these thing together under one big tent.