Distribution of 0 digits in factorial series: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
(Added Go) |
||
Line 41: | Line 41: | ||
permanently falls below 0.16. This task took many hours in the Python example, though I wonder if there is a faster |
permanently falls below 0.16. This task took many hours in the Python example, though I wonder if there is a faster |
||
algorithm out there. |
algorithm out there. |
||
=={{header|Go}}== |
|||
{{libheader|GMP(Go wrapper)}} |
|||
Brute force as I'll be surprised if there is a faster 'exact' algorithm for this task. |
|||
However, the combination of a native code compiler and GMP really cuts down the times (2.8 seconds for the basic task and 182.5 seconds for the stretch goal). Expect these times to be reduced further by the fastest languages. |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
big "github.com/ncw/gmp" |
|||
"rcu" |
|||
) |
|||
func main() { |
|||
fact := big.NewInt(1) |
|||
sum := 0.0 |
|||
first := int64(0) |
|||
firstRatio := 0.0 |
|||
fmt.Println("The mean proportion of zero digits in factorials up to the following are:") |
|||
for n := int64(1); n <= 50000; n++ { |
|||
fact.Mul(fact, big.NewInt(n)) |
|||
bytes := []byte(fact.String()) |
|||
digits := len(bytes) |
|||
zeros := 0 |
|||
for _, b := range bytes { |
|||
if b == '0' { |
|||
zeros++ |
|||
} |
|||
} |
|||
sum += float64(zeros)/float64(digits) |
|||
ratio := sum / float64(n) |
|||
if n == 100 || n == 1000 || n == 10000 { |
|||
fmt.Printf("%6s = %12.10f\n", rcu.Commatize(int(n)), ratio) |
|||
} |
|||
if first > 0 && ratio >= 0.16 { |
|||
first = 0 |
|||
firstRatio = 0.0 |
|||
} else if first == 0 && ratio < 0.16 { |
|||
first = n |
|||
firstRatio = ratio |
|||
} |
|||
} |
|||
fmt.Printf("%6s = %12.10f", rcu.Commatize(int(first)), firstRatio) |
|||
fmt.Println(" (stays below 0.16 after this)") |
|||
fmt.Println(sum/ 50000) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The mean proportion of zero digits in factorials up to the following are: |
|||
100 = 0.2467531862 |
|||
1,000 = 0.2035445511 |
|||
10,000 = 0.1730038482 |
|||
47,332 = 0.1599999958 (stays below 0.16 after this) |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |