Skip to content

Instantly share code, notes, and snippets.

@cdsap
Created December 15, 2022 19:05
Show Gist options
  • Save cdsap/b49a558ceae0f5ab3ad296752bab9cb7 to your computer and use it in GitHub Desktop.
Save cdsap/b49a558ceae0f5ab3ad296752bab9cb7 to your computer and use it in GitHub Desktop.
Benford
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