Вы находитесь на странице: 1из 6

Definition Pascal's Triangle Pascal's triangle is a well known set of numbers aligned in the shape of a pyram id.

The numbers represent the binomial coefficients. Binomial coefficients repre sent the number of subsets of a given size. The numbers in Pascal's triangle are also the coefficients of the expansion of (a+b)n, (a+b) raised to the n-th powe r. So for n equals to three, the expansion is (a+b) x (a+b) x (a+b) which equals (a2+2ab+b2) x (a+b) which equals (a3 + 3ab2 + 3ba2 + b3). The coefficients are 1,3,3,1. History The set of numbers that form Pascal's triangle were well known before Pascal. Ho wever, Pascal developed many applications of it and was the first one to organiz e all the information together in his treatiseThe numbers originally arose from Hindu studies of combinatorics and binomial numbers and the Greeks' study of fig urate numbers. It is named after the French mathematician Blaise Pascal in much of the Western world, although other mathematicians studied it centuries before him in India, Persia, China, Germany, and Italy. Pascal's triangle was also known as the Figurate Triangle, the Combinatorial Tri angle, and the Binomial Triangle. The triangle was first given the name, "Pascal 's triangle," by a mathematician named Montmort in 1708. Montmort wrote the numb ers in the form below known as the combinatorial triangle. It was discussed in Persia (Iran) by the Persian mathematician, Al-Karaji (953 102 9).[4] It was later repeated by the Persian poet-astronomer-mathematician Omar K hayym (1048 1131); thus the triangle is referred to as the Khayyam triangle in Iran . Several theorems related to the triangle were known, including the binomial th eorem. Khayyam used a method of finding nth roots based on the binomial expansio n, and therefore on the binomial coefficients. In 13th century, Yang Hui (1238 1298) presented the arithmetic triangle that is th e same as Pascal's triangle. Pascal's triangle is called Yang Hui's triangle in China. The "Yang Hui's triangle" was known in China in the early 11th century by the Chinese mathematician Jia Xian (1010-1070). In Italy, it is referred to as Tartaglia's triangle, named for the Italian algeb raist Niccol Fontana Tartaglia (1500 77). Tartaglia is credited with the general fo rmula for solving cubic polynomials, (which may in fact be from Scipione del Fer ro but was published by Gerolamo Cardano 1545). How to construct it At the tip of Pascal's Triangle is the number 1, which makes up the zeroth row. The first row (1 & 1) contains two 1's, both formed by adding the two numbers ab ove them to the left and the right, in this case 1 and 0 (all numbers outside th e Triangle are 0's). Do the same to create the 2nd row: 0+1=1; 1+1=2; 1+0=1. And the third: 0+1=1; 1+2=3; 2+1=3; 1+0=1. In this way, the rows of the triangle go on infinitly. A number in the triangle can also be found by nCr (n Choose r) wh ere n is the number of the row and r is the element in that row. For example, in row 3, 1 is the zeroth element, 3 is element number 1, the next three is the 2n d element, and the last 1 is the 3rd element. The formula for nCr is: n! -------r!(n-r)! ! means factorial, or the preceeding number multiplied by all the positive integ ers that are smaller than the number. 5! = 5 4 3 2 1 = 120.

a.The Sums of the Rows The sum of the numbers in any row is equal to 2 to the nth power or 2n, when n i s the number of the row. For example: 20 = 1 21 = 1+1 = 2 22 = 1+2+1 = 4 23 = 1+3+3+1 = 8 24 = 1+4+6+4+1 = 16

b.Prime Numbers There is also an interesting pattern in the triangle dealing with prime numbers. For any prime numbered row, or row where the first element is a prime number, all the numbers in that row (excluding the 1 s) are divisible by the prime. For e xample, in the seventh row (1 7 21 35 35 21 7 1) 7, 21, and 35 are all divisible by 7. Yet, in a composite numbered row, such as row 6 (1 6 15 20 15 6 1), 15 a nd 20 are not divisible by 6. In more mathematical terms it can be stated: if n is a prime number, then all the middle terms (all terms except the two end terms ) of the nth row are divisible by n. On the other hand, if n is a composite num ber, then some terms in the nth row will not be divisible by n (Clawson 134). Th is is yet another pattern found within Pascal s Triangle. c.Hockey Stick Pattern If a diagonal of numbers of any length is selected starting at any of the 1's bo rdering the sides of the triangle and ending on any number inside the triangle o n that diagonal, the sum of the numbers inside the selection is equal to the num ber below the end of the selection that is not on the same diagonal itself. If y ou don't understand that, look at the drawing. 1+6+21+56 = 84 1+7+28+84+210+462+924 = 1716 1+12 = 13

d.Fibonnacci's Sequence Fibonnacci's Sequence can also be located in Pascal's Triangle. The sum of the n umbers in the consecutive rows shown in the diagram are the first numbers of the Fibonnacci Sequence. The Sequence can also be formed in a more direct way, very similar to the method used to form the Triangle, by adding two consecutive numb ers in the sequence to produce the next number. The creates the sequence: 1,1,2, 3,5,8,13,21,34, 55,89,144,233, etc . . . . The Fibonnacci Sequence can be found

in the Golden Rectangle, the lengths of the segments of a pentagram, and in natu re, and it decribes a curve which can be found in string instruments, such as th e curve of a grand piano. The formula for the nth number in the Fibonnacci Seque nce is Answer: The Applet tag is used to embed an applet in an HTML document; theApplet tag ta kes zero or more parameters. The Applet Tag The Applet tag is written the Body tag of an HTML document. Syntax<APPLETCODE = name of the class file that extends java.applet.Applet CODEBASE = path of the class file HEIGHT = maximum height of the applet, in pixels WIDTH = ximum width of the applet, in pixels VSPACE = vertical space between the applet an d the rest of the HTML HSPACE = horizontal space between the applet and the rest of theHTML ALIGN = alignment of the applet with respect to the rest of the webpage AL T = alternate text to be displayed if the browser does not supportapplets ><PARAM NAME= parameter_name value= value_of_parameter > ..</APPLET> The most commonly used attributes of the Applet tag are CODE, HEIGHT, WIDTH,COD EBASE and ALT. You can send parameters to the applet using the PARAMtag. The PAR AM tag must be written between <APPLET> and </APPLET>Example:<AppletCode = clock. class Height = 200Width = 200 > </applet> Life Cycle of an Applet You can describe the life cycle of an applet through four methods. Thesemethods are: ? The init () method. ? The start () method. ? The stop () method. ? The destroy () method. The init ( ) method The init () method is called the first time an applet is loaded into the memory of a computer. You can initialize variables, and add components like buttons an dcheck boxes to the applet in the init () method. The start ( ) method The start () method is called immediately after the init () method and every ti methe applet receives focus as a result of scrolling in the active window. You c anuse this method when you want to restart a process, such as thread animation,e very time the applet receives the focus. The stop ( ) method The stop () method is called every time the applet loses the focus. You can use this method to reset variables and stop the threads that are running. The destroy ( ) method The destroy ( ) method is called by the browser when the user moves to anotherp age. You can use this method to perform clean-up operations like closing a file. The following diagram depicts the life cycle of an applet.It is not mandatory t o use any or all the methods of the applet. These methodsare called automaticall y by the Java environment, and hence, must be declaredpublic. None of the method s accept parameters. For example, public void init (){}All but the most trivial applets override a set of methods that provides the basicmechanism by which the browser or applet viewer interface s to the applet andcontrols its execution. Four of these methods init( ) , start( ) , stop( )

ma

, and destroy( ) are defined by Applet . Another, paint( ) , is defined by the AWT Component class. Default implementations for all of these methods areprovided. Applets do not need to override those methods they do not use.However, only very simple app lets will not need to define all of them. These fivemethods can be assembled int o the skeleton shown here:// An Applet skeleton.import java.awt.*;import java.ap plet.*;/*<applet code="AppletSkel" width=300 height=100></applet>*/public class AppletSkel extends Applet {// Called first.public void init() {// initialization }/* Called second, after init(). Also called wheneverthe applet is restarted. */ public void start() {// start or resume execution}// Called when the applet is s toppedpublic void stop() {// suspends execution}/* Called when applet is termina ted. This is the lastmethod executed. */public void destroy() {// perform shutdo wn activities Q.2 Write a short note on following topic:a. Knock Knock Protocolb. DatagramAnsw er: A The KnockKnockProtocol class implements the protocol that the client andserver use to communicate. This class keeps track of where the client and theserver are in their conversation a nd serves up the server's response to theclient's statements. The KnockKnockServ er object contains the text of all the jokes and makes sure that the client give s the proper response to the server'sstatements. It wouldn't do to have the clie nt say "Dexter who?" when the serversays "Knock! Knock!"All client/server pairs must have some protocol by which they speak to eachother; otherwise, the data th at passes back and forth would be meaningless. Theprotocol that your own clients and servers use depends entirely on thecommunication required by them to accomp lish the task. Answer: B Clients and servers that communicate via a reliable channel, such as a TCPsocket , have a dedicated point-to-point channel between themselves, or at leastthe ill usion of one. To communicate, they establish a connection, transmit thedata, and then close the connection. All data sent over the channel is received inthe sam e order in which it was sent. This is guaranteed by the channel.In contrast, app lications that communicate via datagrams send and receivecompletely independent packets of information. These clients and servers do nothave and do not need a d edicated point-to-point channel. The delivery of datagrams to their destinations is not guaranteed. Nor is the order of theirarrival. Definition: A datagram is an independent, self-contained message sent overthe network whose arrival, ar rival time, and content are not guaranteed. The java.net package contains three classes to help you write Java programs tha tuse datagrams to send and receive packets over the network: DatagramSocket,Data gramPacket, and MulticastSocketAn application can send and receiveDatagramPacket s through a DatagramSocket. In addition, DatagramPackets canbe broadcast to mult iple recipients all listening to a MulticastSocket. Writing a Datagram Client and Server The example featured in this section consists of two applications: a client and aserver. The server continuously receives datagram packets over a datagramsocke t. Each datagram packet received by the server indicates a client requestfor a q

uotation. When the server receives a datagram, it replies by sending adatagram p acket that contains a one-line "quote of the moment" back to theclient. The clie nt application in this example is fairly simple. It sends a single datagrampacke t to the server indicating that the client would like to receive a quote of them oment. The client then waits for the server to send a datagram packet inresponse . Two classes implement the server application: QuoteServer andQuoteServerThread . A single class implements the client application:QuoteClient.Let s investigate t hese classes, starting with the class that contains the mainmethod for the serve r application. Working with a Server-Side Applicationcontains an applet version of the QuoteClient class. The QuoteServer Class The QuoteServer class, shown here in its entirety, contains a single method: th emain method for the quote server application. The main method simply creates an ew QuoteServerThread object and starts it:import java.io.*;public class QuoteSer ver {public static void main(String[] args) throws IOException {new QuoteServerT hread().start();}} The QuoteServerThread class implements the main log super(name);socket = new DatagramSocket(4445); try {in = new BufferedReader(new FileReader("one-liners.txt"));} catch (FileNotFoundException e)System.err.printl n("Couldn't open quote file. " +"Serving time instead.");}}Remember that certain ports are dedicated to well-known services and youcannot use them. If you speci fy a port that is in use, the creation of theDatagramSocket will fail. The const ructor also opens a BufferedReader on a file named one-liners.txtwhich contains a list of quotes. Each quote in the file is on a line by itself.Now for the inte resting part of the QuoteServerThread: its run method. The runmethod overrides r un in the Thread class and provides the implementation forthe thread. For inform ation about threads, see Defining and Starting a Thread. The run method contains a while loop that continues as long as there are morequotes in the file. During each iteration of the loop, the thread waits for aDatagramPacket to arrive over the DatagramSocket. The packet indicates arequest from a client. In response to the client s request, the QuoteServerThreadgets a quote from the file, puts it in a DatagramPacket and sends it over theDatagramSocket to the client that asked f or it.Let s look first at the section that receives the requests from clients:byte [] buf = new byte[256];DatagramPacket packet = new DatagramPacket(buf, buf.lengt h);socket.receive(packet); The first statement creates an array of bytes which i s then used to create aDatagramPacket. The DatagramPacket will be used to receiv e a datagram fromthe socket because of the constructor used to create it. This c onstructor requiresonly two arguments: a byte array that contains client-specifi c data and thelength of the byte array. When constructing a DatagramPacket to se nd over theDatagramSocket, you also must supply the Internet address and port nu mber of the packet s destination. You ll see this later when we discuss how the serv erresponds to a client request. The last statement in the previous code snippet receives a datagram from thesocket (the information received from the client get s copied into the packet). Thereceive method waits forever until a packet is rec eived. If no packet is received,the server makes no further progress and just wa its.Now assume that, the server has received a request from a client for a quote .Now the server must respond. This section of code in the run method constructst he response:String dString = null;if (in == null)dString = new Date().toString() ;elsedString = getNextQuote();buf = dString.getBytes();If the quote file did not get opened for some reason, then in equals null. If this isthe case, the quote server serves up the time of day instead. Otherwise, thequote server gets the ne xt quote from the already opened file. Finally, the codeconverts the string to a n array of bytes.Now, the run method sends the response to the client over the D atagramSocketwith this code:InetAddress address = packet.getAddress();int port = packet.getPort();packet = new DatagramPacket(buf, buf.length, address, port);so cket.send(packet); The first two statements in this code segment get the Interne t address and theport number, respectively, from the datagram packet received fr om the client. The Internet address and port number indicate where the datagram packet camefrom. This is where the server must send its response. In this exampl e, the bytearray of the datagram packet contains no relevant information. The ar rival of thepacket itself indicates a request from a client that can be found at

the Internetaddress and port number indicated in the datagram packet. The third statement creates a new DatagramPacket object intended for sendinga datagram me ssage over the datagram socket. You can tell that the newDatagramPacket is inten ded to send data over the socket because of theconstructor used to create it. Th is constructor requires four arguments. The firsttwo arguments are the same requ ired by the constructor used to createreceiving datagrams: a byte array containi ng the message from the sender tothe receiver and the length of this array. The next two arguments are different:an Internet address and a port number. These tw o arguments are the completeaddress of the destination of the datagram packet an d must be supplied by thesender of the datagram. The last line of code sends the DatagramPacket on itsway.When the server has read all the quotes from the quote file, the while loopterminates and the run method cleans up:socket.close(); The QuoteClient Class TheQuoteClientclass implements a client application for the QuoteServer. Thisap plication sends a request to the QuoteServer, waits for the response, and,when t he response is received, displays it to the standard output. Let s look atthe code in detail

Вам также может понравиться