Created
December 15, 2022 19:05
-
-
Save cdsap/b49a558ceae0f5ab3ad296752bab9cb7 to your computer and use it in GitHub Desktop.
Benford
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Benford(sample: Array<BigInteger>) { | |
override fun toString() = str | |
private val firstDigits = IntArray(9) | |
private val count = sample.size.toDouble() | |
private val str: String | |
init { | |
for (n in sample) { | |
firstDigits[n.toString().substring(0, 1).toInt() - 1]++ | |
} | |
str = with(StringBuilder()) { | |
for (i in firstDigits.indices) { | |
append(i + 1).append('\t').append(firstDigits[i] / count) | |
append('\t').append(Math.log10(1 + 1.0 / (i + 1))).append('\n') | |
} | |
toString() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment