Problem Statement
Pick a random financial transaction from the ledger of a typical business and there is about a 30% chance that the first non-zero digit of the amount of money involved is a 1. This counter-intuitive fact is a result of Benford's Law, discovered by astronomer Simon Newcomb in the late 1800's and rediscovered by physicist Frank Benford in the early 1900's. They found that real world data are not evenly distributed. Instead, given a random number related to some natural or social phenomenon satisfying certain conditions, the probability that the first non-zero digit of the number is D is log10(1 + 1/D).
Increasingly, financial auditors are using Benford's Law to detect possible fraud. Given a record of the financial transactions of a company, if some leading digit appears with a frequency that significantly differs from that predicted by Benford's Law, it is a signal that those transactions deserve a closer look.
Create a class BenfordsLaw with a method questionableDigit
that takes a
For example, consider the data
5236 7290 200 1907 3336 9182 17 4209 8746 7932 6375 909 2189 3977 2389 2500 1239 3448 6380 4812The following chart gives the actual frequency of each leading digit, and its expected frequency according to Benford's law.
digit | 1 2 3 4 5 6 7 8 9 ---------|--------------------------------------------- actual | 3 4 3 2 1 2 2 1 2 expected | 6.02 3.52 2.50 1.94 1.58 1.34 1.16 1.02 0.92Assuming a threshold of 2, there are two digits that are questionable: the leading digit 1 appears less than half as often as expected and the leading digit 9 appears more than twice as often as expected. Since 1 is smaller than 9, the answer is 1.
Note that, although the expected frequencies have been rounded to two decimal places in the above table for the purposes of presentation, you should perform all such calculations without rounding. Errors of up to 0.000001 in the expected frequencies will not affect the final answer.
Definition
- Class:
- BenfordsLaw
- Method:
- questionableDigit
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int questionableDigit(int[] transactions, int threshold)
- (be sure your method is public)
Notes
- In the formula for Benford's Law, log10 means the base-10 logarithm.
Constraints
- transactions contains between 1 and 50 elements, inclusive.
- Each element of transactions is between 1 and 999999999, inclusive.
- threshold is between 2 and 10, inclusive.
- Errors of up to 0.000001 in calculating the expected frequencies will not affect the final answer.
Examples
{ 5236,7290,200,1907,3336,9182,17,4209,8746,7932, 6375,909,2189,3977,2389,2500,1239,3448,6380,4812 }
2
Returns: 1
The example above.
{ 1,10,100,2,20,200,2000,3,30,300 }
2
Returns: 2
2 and 3 appear more than twice as often as expected, and 4 through 9 appear less than half as often as expected. 2 is the smallest of these digits.
{ 9,87,765,6543,54321,43219,321987,21987,1987,345,234,123 }
2
Returns: -1
{ 1,2,3,4,5,6,7,8,7,6,5,4,3,2,1 }
8
Returns: 9
{ 987,234,1234,234873487,876,234562,17, 7575734,5555,4210,678234,3999,8123 }
3
Returns: 8
{168249,128764,274843,480487,6470,321163,914970,651946,860681,651156,533305,97811,948843,357358,312413,255070,74193,28869,793437,592639,25154,554199,458379,585723,957071,620787,618315,999088,992120,660875}
5
Returns: -1
{929238,225022,368132,419489,406037,476829,454825,792981,873000,566777,925392,448354,270943,717454,441466,943277,981218,825975,529528,884725,366158,409775,286906,97902,136183,644910,660556,268189,344640,310889}
9
Returns: 1
{87686,300702,161999,917426,475725,725295,312782,410472,237840,75515, 674516,391965,562072,154635,359049,934178,555142,159623,623961,864710, 617694,108332,828539,602937,170762,543943,461252,582121,176235,789195}
3
Returns: 2
{935189,872499,767283,621491,284705,445848,187047, 410621,215923,116325,926201,764755,175689,982944, 906067,427226,227093,691445,937981,182991,11663,916071,852174,503223,601235,543188,21146, 36881,37372,384399,10735,386812,527127,99382,108651, 851071,396242,892414,778132,784572}
2
Returns: 9
{2,8,2,2,138836868,4,12,157,19,3,102,879,5,5,35,4,4,19,666,24,123,7,937,209,6,7,2,3,100,3,4,3,4,111,4,2,4}
4
Returns: -1
{46126,6669,10,9,3,8,401,4,20947581,3,81,10,101023,1770,14,8,383,127,5,41,654,7,9,94,2,32,240,1949969,3,3,4,4,256,4,2268,2,3}
2
Returns: 5
{234,5,38,3,5,4,8,9,18,3,20,3,2,4,1968049,27,30,3,2,2,2,4,3,534914,4,10,37,70,3,3,2152,27,8,1533,1217,3,3,2,23,3,567,4,10,130004,3,5,5371,10253521,3}
10
Returns: 6
{22,26,47822,10,68597,1201,22983,9,22,263,11,80,1108,36151,20,43,22,15,13958,5285,9,23,4886,10157537,2471,90,8,26,32,2180,68,70,2498,16,31,8,21,8258,26,15,9,30306576,52,10,82,10,81,720}
2
Returns: 8
{465,10,73,64,17,1618,29,671869,458,17,12,17,12,9,7,18,10,9,137087639,22820083,10,14,11,60,19,1615,911,46,28,534546,232,54,3138972,10,27,11,740226530,22,9242,16,9,12,36,8,15}
3
Returns: -1
{12,35847144,289578,23,8,49,16,27,73,9,11,604,12,7,38,25,10,6428,22965,9,12,11,39,30,18,14,11,16,503,92,21474,8,20,25,45122636,157,8,8,14,458,292,545003208,13}
6
Returns: -1
{34,3934,35,228,730,8,5321491,30,6324,15,2274,11,76,11,11,106382,613,37}
3
Returns: 4
{1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,1,2}
9
Returns: -1
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,4,4, 1,2,3,4,5,4,4,4,4, 4,4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4 }
6
Returns: -1
{ 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 1,2,3,4,5,7,8,9,1,2,3,4,5 }
8
Returns: 6
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,7,7,7,7,7, 1,2,3,7,7 }
6
Returns: -1
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,8,8,8,8,8, 1,2,8,8 }
8
Returns: -1
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,7,7, 1,2,3,4,5 }
3
Returns: -1
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,8, 1,2,3,4,5,6,8,8,8, 1,2,3,4,5,6,8,8,8, 1,2,3,4,5,8,8 } }
5
Returns: 8
{ 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,3,3,4,5,6,7,1,1, 1,1,1,4,5,6,7,1,1 }
2
Returns: 2
{1}
4
Returns: 2
{1}
3
Returns: 1
{ 987, 234, 1234, 234873487, 876, 234562, 17, 7575734, 5555, 4210, 678234, 3999, 8123 }
3
Returns: 8
{ 1, 10, 100, 2, 20, 200, 2000, 3, 30, 300 }
2
Returns: 2
{ 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1 }
8
Returns: 9
{ 1 }
2
Returns: 1
{ 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 9876, 6543 }
8
Returns: -1
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2, 3, 1 }
10
Returns: -1