Distribution of 0 digits in factorial series: Difference between revisions

Add C# implementation
m (Added language identifier.)
(Add C# implementation)
 
(One intermediate revision by one other user not shown)
Line 151:
1000 : 0.2035445511031646
10000 : 0.1730038482418671</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Numerics;
 
public class DistributionInFactorials
{
public static void Main(string[] args)
{
List<int> limits = new List<int> { 100, 1_000, 10_000 };
foreach (int limit in limits)
{
MeanFactorialDigits(limit);
}
}
 
private static void MeanFactorialDigits(int limit)
{
BigInteger factorial = BigInteger.One;
double proportionSum = 0.0;
double proportionMean = 0.0;
 
for (int n = 1; n <= limit; n++)
{
factorial = factorial * n;
string factorialString = factorial.ToString();
int digitCount = factorialString.Length;
long zeroCount = factorialString.Split('0').Length - 1;
proportionSum += (double)zeroCount / digitCount;
proportionMean = proportionSum / n;
}
 
string result = string.Format("{0:F8}", proportionMean);
Console.WriteLine("Mean proportion of zero digits in factorials from 1 to " + limit + " is " + result);
}
}
</syntaxhighlight>
{{out}}
<pre>
Mean proportion of zero digits in factorials from 1 to 100 is 0.24675319
Mean proportion of zero digits in factorials from 1 to 1000 is 0.20354455
Mean proportion of zero digits in factorials from 1 to 10000 is 0.17300385
</pre>
 
=={{header|C++}}==
Line 1,335 ⟶ 1,381:
{{libheader|Wren-fmt}}
Very slow indeed, 10.75 minutes to reach N = 10,000.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var fact = BigInt.one
Line 1,363 ⟶ 1,409:
{{trans|Phix}}
Around 60 times faster than before with 10,000 now being reached in about 10.5 seconds. Even the stretch goal is now viable and comes in at 5 minutes 41 seconds.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var rfs = [1] // reverse factorial(1) in base 1000
337

edits