Distribution of 0 digits in factorial series: Difference between revisions

(Added Go)
Line 97:
47,332 = 0.1599999958 (stays below 0.16 after this)
</pre>
 
=={{header|Nim}}==
 
===Task===
{{libheader|bignum}}
<lang Nim>import strutils, std/monotimes
import bignum
 
let t0 = getMonoTime()
var sum = 0.0
var f = newInt(1)
var lim = 100
for n in 1..10_000:
f *= n
let str = $f
sum += str.count('0') / str.len
if n == lim:
echo n, ":\t", sum / float(n)
lim *= 10
echo()
echo getMonoTime() - t0</lang>
 
{{out}}
<pre>100: 0.2467531861674322
1000: 0.2035445511031646
10000: 0.1730038482418671
 
(seconds: 2, nanosecond: 857794404)</pre>
 
===Stretch task===
{{libheader|bignum}}
At each step, we eliminate the trailing zeroes to reduce the length of the number and save some time. But this is not much, about 8%.
 
<lang Nim>import strutils, std/monotimes
import bignum
 
let t0 = getMonoTime()
var sum = 0.0
var first = 0
var f = newInt(1)
var count0 = 0
for n in 1..<50_000:
f *= n
while f mod 10 == 0: # Reduce the length of "f".
f = f div 10
inc count0
let str = $f
sum += (str.count('0') + count0) / (str.len + count0)
if sum / float(n) < 0.16:
if first == 0: first = n
else:
first = 0
 
echo "Permanently below 0.16 at n = ", first
echo "Execution time: ", getMonoTime() - t0</lang>
 
{{out}}
<pre>Permanently below 0.16 at n = 47332
Execution time: (seconds: 190, nanosecond: 215845101)</pre>
 
=={{header|Python}}==
Anonymous user