Java Reference
In-Depth Information
43 System.out.printf("Total %5d %6.2f\n", total, 100.0);
44 }
45
46 // returns the sum of the integers in the given array
47 public static int sum( int [] data) {
48 int sum = 0;
49 for ( int n : data) {
50 sum += n;
51 }
52 return sum;
53 }
54
55 // returns the first digit of the given number
56 public static int firstDigit( int n) {
57 int result = Math.abs(n);
58 while (result >= 10) {
59 result = result / 10;
60 }
61 return result;
62 }
63 }
Now that we have a complete program, let's see what we get when we analyze
various data sets. The Benford distribution shows up with population data because
population tends to grow exponentially. Let's use data from the web page http://www.
census.gov/popest/counties/ which contains population estimates for various U.S.
counties. The data set has information on 3139 different counties with populations
varying from 67 individuals to over 9 million for the census year 2000. Here is a
sample output of our program using these data:
Let's count those leading digits...
input file name? county.txt
Digit Count Percent
1 970 30.90
2 564 17.97
3 399 12.71
4 306 9.75
5 206 6.56
6 208 6.63
7 170 5.24
8 172 5.48
9 144 4.59
Total 3139 100.00
Search WWH ::




Custom Search