Entropy/Narcissist: Difference between revisions

Content added Content deleted
(Added C++ version)
Line 583: Line 583:
{{out}}
{{out}}
<pre>4.3591214356783</pre>
<pre>4.3591214356783</pre>

=={{header|Nim}}==
<lang Nim>import os, math, strutils, tables

let execName = getAppFilename().splitPath().tail
let srcName = execName & ".nim"

func entropy(str: string): float =
var counts: CountTable[char]
for ch in str:
counts.inc(ch)
for count in counts.values:
result -= count / str.len * log2(count / str.len)

echo "Source file entropy: ", srcName.readFile().entropy().formatFloat(ffDecimal, 5)
echo "Binary file entropy: ", execName.readFile().entropy().formatFloat(ffDecimal, 5)</lang>

{{out}}
<pre>Source file entropy: 4.75555
Binary file entropy: 5.97036</pre>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==