In the natural world we find many hidden, mathematical patterns. You can find the Fibonacci sequence in conch shells, fractals in plants, and the Golden Ratio in great works of architecture such as the Great Mosque of Kairouan. In this assignment, you will explore another pattern that shows up in some (large) sets of numbers: Benford's Law.

When looking at large sets of data, you would assume that the first digits of any large set of numbers would be roughly evenly distributed between 1 and 9. In fact, it is likely that about 30% of the numbers start with a 1 (vs. 5% of the numbers will start with a 9). Benford's Law includes an equation to calculate expected frequencies of the leading digits of data, and you can read more about that equation and its proof if you are interested.

Benford's Law could be just an interesting pattern that shows up in unexpected places (I found it when I was researching the popularity of World of Warcraft mods ....), but it does have a valuable use in detecting fraud in various types of data, particularly financial data, since people tend to try to construct fake data using numbers with an equal distribution of digits. It does have other uses as well, as you can read about in the article linked above.

For now, we will simply use this law as a way to practice a few programming skills: reading a data file, using modulo arithmetic to process numbers, looping, and managing arrays.

Your program will need to:

- open a file for reading
- process numbers to identify the leading (i.e. leftmost) digit for each number in the file. There are a variety of ways to do this.
- record the number of times each digit between 1 and 9 was seen as the leading digit of a number in the file
- report the results as a table of frequencies or a histogram or both.

In addition, you should analyze the results of the program and evaluate whether or not your testing data fits with the expected ratios given by Benford's Law. If you are able to do the calculations for the log function, go ahead. Otherwise, you can draw a histogram and see if the distribution looks approximately like the one given in the article referenced above.

Benford's Law does fit many collections of naturally occuring data but not all. If your data does not fit, don't worry. Just report your results .... this is what often happens in experimentation, even in computer science.

Your program should be able to read in a file of numbers that are listed with one number per line. You do not know how many numbers there will be in the file. This is a short example file for initial testing: benfordexample.txt. This is a list of enrollments in Research Park Triangle colleges in the Fall of 2000.

You will probably want to be able to specify the file name on the command line. Alternatively, you could make the file name a string that is requested by the program when it starts running.

At a minimum, your program should print a table of digits and the frequency, such as:

```
```
Digit
Frequency
1
3
2
4
3
1
4
0
5
2
6
1
7
0
8
0
9
0

You can also gain extra points by figuring out how to print a histogram (which can be oriented sideways to make it easier to print in C).

You should test your program on multiple data sets. You can use the benfordexample.txt to start, but then you should test with larger data files. You can find one on your own (give acknowledgement where you got it!), or you can use one or more of these:

- LiveJournal starts collected by Shirley Man. The number of new LiveJournal accounts created daily Jan 1, 2000–Feb 28, 2005.
- Library holdings collected by Christian Ayotte. The number of books held in a set of several thousand US libraries, from an Access database
- Sunspot data collected by Robin McQuinn. The number of sunspots that occur monthly, as compiled by a Belgian agency

Note that you will need to "clean up" these data files before proceeding. The first two files just need the first few lines removed. The third one will need substantial preprocessing to remove or ignore the string between the parentheses.

You should include:

- A test PLAN
- List of data sources you will use in testing, but I do NOT need to see the data itself. That would take far too many pages!!!
- A test script showing testing of several test cases. Do NOT have the program echo the input to the output.
- Based on your tests, do you think Benford's Law applied to your data sets?

This project will be worth up to 15 extra credit points, according to the following rubric.

- [2 points] Opens appropriate file for reading. File name specified either on the command line or at a prompt from the program
- [3 points]Program correctly identifies the leading (leftmost) digit of each number in the file
- [1 point] Program tracks frequency of the leading digit for each number in the file
- [1 point] Program prints a table of frequencies of the leading digit for each number in the file
- [2 points] Program prints a histogram of the leading digit for each number in the file
- [4 points] Testing
- Test plan included
- Data sources identified
- Student-provided data source used
- Script showing several runs of the program

- [2 points] Analysis of applicability of Benford's Law to the data sets tested

This project is an adaptation of the Natural Prestidigitation assignment from Stanford's Nifty Assignments collection, including sample data file links.