Distribution of 0 digits in factorial series: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
(Add C# implementation) |
||
Line 151: | Line 151: | ||
1000 : 0.2035445511031646 |
1000 : 0.2035445511031646 |
||
10000 : 0.1730038482418671</pre> |
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++}}== |
=={{header|C++}}== |