Monday, 26 September 2011

A program for moving cars


import java.awt.*;
import java.util.*;
import java.applet.*;
/*
 <APPLET CODE="Animation.java" WIDTH=400 HEIGHT=300>
 </APPLET>
*/
//The basic applet class.The applet shows 4 cars crossing each other at a square.
 public class Animation extends Applet implements Runnable
 {
 Thread t;
 //4 variables used to vary the car's positions.
 int x1=0,x2=380,y1=50,y2=250;
 public void start()
 {
   if(t==null)
   {
   t=new Thread(this,"New Thread");//New side Thread created on start of applet.
   t.start();
   }
 }
  public void stop()
  {
   if(t!=null)
   {
   t=null;//On stop of applet the created thread is destroyed.
   }
  }


 //Implementation of method run() of Runnable interface.


  public void run()
  {
  Thread t1=Thread.currentThread();
  while(t==t1)
  {
   repaint();
   try
   {
    Thread.sleep(100);
   }
   catch(Exception e)
   {   }
   }
 }
 public void paint(Graphics g)
 {
  setBackground(Color.cyan);
  g.setColor(Color.BLACK);
  x1=(x1+16)%400;
  x2=x2-16;
  y1=(y1+12)%300;
  y2=y2-12;
  if(y2<0)
   y2=288;
  if(x2<0)
   x2=384;
  //Draw the roads using 2 filled rectangles using black color.
  g.fillRect(0,130,400,40);
  g.fillRect(180,0,40,305);
  //Draw the white colored lines.
  g.setColor(Color.white);
  for(int i=0;i<20;i++)
  {
   if(i!=9 && i!=10)
    g.drawLine(i*20,150,i*20+10,150);
  }
  for(int j=0;j<15;j++)
  {
   if(j!=7 && j!=8)
    g.drawLine(200,j*20,200,j*20+10);
  }
  //Draw 4 colored cars using filled round rectangles.
  g.setColor(Color.red);
  g.fillRoundRect(x2,152,20,8,2,2);
  g.fillRoundRect(x1,140,20,8,2,2);
  g.fillRoundRect(190,y1,8,20,2,2);
  g.fillRoundRect(202,y2,8,20,2,2);
 }
}

What is an Armstrong number ?


Armstrong number



Also known as narcissistic numbers, Armstrong numbers are the sum of their own digits to the power of the number of digits. As that is a slightly brief wording, let me give an example:
153 = 1³ + 5³ + 3³
Each digit is raised to the power three because 153 has three digits. They are totalled and we get the original number again! Notice that Armstrong numbers are base dependent, but we'll mainly be dealing with base 10 examples here.
Armstrong numbers are certainly rare. They cannot have more than 60 digits in base 10, because for n > 60
n · 9n < 10n-1
Since there is an upper limit to their size, it is theoretically possible to find all of them, given sufficient computer time. However, 1060 is an unimaginably huge number, so such a "brute force" approach would be unwise. Luckily, D. Winter proved in 1985 that there are exactly 88 base-10 Armstrong numbers, and they must have 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 16, 17, 19, 20, 21, 23, 24, 25, 27, 29, 31, 32, 33, 34, 35, 37, 38 or 39 digits. Of course, the one digit Armstrong numbers are somewhat trivial since clearly 11 = 1, 21 = 2 etc.
The Armstrong numbers up to 10 digits are
1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774
The largest Armstrong number (in base 10) is the 39-digit beast:
115132219018763992565095597973971522401.