Distribution of 0 digits in factorial series: Difference between revisions

Add C# implementation
m (remove draft tag)
(Add C# implementation)
 
(6 intermediate revisions by 4 users not shown)
Line 132:
The mean proportion dips permanently below 0.16 at 47332.
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">su: 0.0
f: 1
lim: 100
loop 1..10000 'n [
'f * n
str: to :string f
'su + (enumerate str 'c -> c = `0`) // size str
if n = lim [
print [n ":" su // n]
'lim * 10
]
]</syntaxhighlight>
 
{{out}}
 
<pre>100 : 0.2467531861674322
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++}}==
{{trans|Phix}}
Line 213 ⟶ 279:
The mean proportion dips permanently below 0.16 at 47332. (4598ms)
</pre>
 
=={{header|Go}}==
===Brute force===
Line 366 ⟶ 433:
Same as 'brute force' version.
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.math.BigInteger;
import java.util.List;
 
public final class DistributionInFactorials {
 
public static void main(String[] aArgs) {
List<Integer> limits = List.of( 100, 1_000, 10_000 );
for ( Integer limit : limits ) {
meanFactorialDigits(limit);
}
}
private static void meanFactorialDigits(Integer aLimit) {
BigInteger factorial = BigInteger.ONE;
double proportionSum = 0.0;
double proportionMean = 0.0;
for ( int n = 1; n <= aLimit; n++ ) {
factorial = factorial.multiply(BigInteger.valueOf(n));
String factorialString = factorial.toString();
int digitCount = factorialString.length();
long zeroCount = factorialString.chars().filter( ch -> ch == '0' ).count();
proportionSum += (double) zeroCount / digitCount;
proportionMean = proportionSum / n;
}
String result = String.format("%.8f", proportionMean);
System.out.println("Mean proportion of zero digits in factorials from 1 to " + aLimit + " 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|jq}}==
'''Works with jq'''
Line 1,226 ⟶ 1,336:
Mean proportion of zero digits in factorials to 10000 is 0.1730038482. (149ms)
The mean proportion dips permanently below 0.16 at 47332. (4485ms)
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">[100, 1000, 10_000].each do |n|
v = 1
total_proportion = (1..n).sum do |k|
v *= k
digits = v.digits
Rational(digits.count(0), digits.size)
end
puts "The mean proportion of 0 in factorials from 1 to #{n} is #{(total_proportion/n).to_f}."
end</syntaxhighlight>
{{out}}
<pre>The mean proportion of 0 in factorials from 1 to 100 is 0.24675318616743222.
The mean proportion of 0 in factorials from 1 to 1000 is 0.20354455110316463.
The mean proportion of 0 in factorials from 1 to 10000 is 0.17300384824186604.
</pre>
=={{header|Sidef}}==
Line 1,250 ⟶ 1,375:
0.173003848241866053180036642893070615681027880906
</pre>
 
=={{header|Wren}}==
===Brute force===
Line 1,255 ⟶ 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,283 ⟶ 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