Beruflich Dokumente
Kultur Dokumente
Program 2.2.5
55
% javac Harmonic.java
% java Harmonic 2
1.5
% java Harmonic 10
2.9289682539682538
% java Harmonic 10000
9.787606036044348
Finite sum. The computational paradigm used by Program 2.2.3 is one that
you will use frequently. It uses two variablesone as an index that controls a
loop and the other to accumulate a computational result.
Class Harmonic in Program 2.2.5 uses
1
the same paradigm to evaluate the sum
1
1 1 1
H N = 1 + --- + --- + --- + + ---1/2
N
2 3 4
1/3
1/5
56
Program 2.2.6
% javac Sqrt.java
% java Sqrt 2
1.414213562373095
java Sqrt 1048575
1023.9995117186336
Computing the square root. How are functions in Javas Math library such as
Math.sqrt implemented? Class Sqrt in Program 2.2.6 illustrates one technique. To compute the square root function, it uses an iterative computation
that is derived from a general computational technique that was developed in
the 17th century by Isaac Newton and Joseph Raphson and is widely known
as Newtons method.
Under generous conditions on a given funcy = f(x)
tion, Newtons method is an effective way to
find roots (values of x for which the function
is 0): Start with an initial estimate t 0 . Given
an estimate t i , compute a new estimate by
root
drawing a line tangent to the curve y = f ( x )
at the point (t i , f ( t i )) and set t i + 1 to the xcoordinate of the point where that line hits
t
t
t
the x axis. As we iterate this process, we get
Newtons method
closer to the root.
i+2
i+1
57
c/t
2.0000000000000000
1.0
1.5000000000000000
1.3333333333333333
1.4166666666666665
1.4117647058823530
1.4142156862745097
1.4142114384748700
1.4142135623746899
1.4142135623715002
1.4142135623730950
1.4142135623730951
58
Program 2.2.7
This program prints the binary representation of the command-line argument, by casting out powers of 2 in
decreasing order (see text). Note that
the first for loop has a null loop body
and the second for loop has a null
initialization statement.
% javac Binary.java
% java Binary 5
101
% java Binary 106
11001010
% java Binary 100000000
101111101011110000100000000
Number conversion. Class Binary in Program 2.2.7 prints the binary (base 2)
representation of the decimal number typed as the command-line argument.
It is based on decomposing the number into a sum of powers of two. For
example, the binary representation of 106 is 1101010, which is the same as
saying that 106 = 64+32+8+2. In binary, this equation is more transparent:
+
+
+
1000000
100000
1000
10
= 1101010
59
binary
representation
binary
representation
N < v
output
106
1101010
64
1000000
false
42
101010
32
100000
false
10
01010
16
10000
true
10
1010
1000
false
010
100
true
10
10
false
true
Read from top to bottom inthe rightmost column, the output is 1101010, the
binary representation of 106.
Converting data from one representation to another is a frequent
theme in writing computer programs. Thinking about conversion emphasizes the distinction between an abstraction (an integer like the number of
hours in a day) and a representation of that abstraction (24 or 11000). The
irony in this case is that the computers representation of an integer is actually
based on its binary representation, as we shall see in Chapter 5.
win
goal
stake
0
goal
stake
lose