Java Reference
In-Depth Information
26 public static void print( int [][] triangle) {
27 for ( int i = 0; i < triangle.length; i++) {
28 for ( int j = 0; j < triangle[i].length; j++) {
29 System.out.print(triangle[i][j] + " ");
30 }
31 System.out.println();
32 }
33 }
34 }
This program produces the following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
7.6 Case Study: Benford's Law
Let's look at a more complex program example that involves using arrays. When you
study real-world data you will often come across a curious result that is known as
Benford's Law , named after a physicist named Frank Benford who stated it in 1938.
Benford's Law involves looking at the first digit of a series of numbers. For example,
suppose that you were to use a random number generator to generate integers in the
range of 100 to 999 and you looked at how often the number begins with 1, how
often it begins with 2, and so on. Any decent random number generator would spread
the answers out evenly among the nine different regions, so we'd expect to see each
digit about one-ninth of the time (11.1%). But with a lot of real-world data, we see a
very different distribution.
When we examine data that matches the Benford distribution, we see a first digit
of 1 over 30% of the time (almost one third) and, at the other extreme, a first digit of
9 only about 4.6% of the time (less than one in twenty cases). Table 7.3 shows the
expected distribution for data that follows Benford's Law.
Why would the distribution turn out this way? Why so many 1s? Why so few 9s? The
answer is that exponential sequences have different properties than simple linear
sequences. In particular, exponential sequences have a lot more numbers that begin with 1.
 
Search WWH ::




Custom Search