From the Math Forum problem of the week 1020:

Let p(n) be the probability that a random n-digit integer has all 10 digits occurring (where for simplicity we do include leading 0s; that is, we consider 0000012345 as being a 10-digit number). So p(9) is 0 and p(10) = 10!/1010.

What is the smallest n for which p(n) > 1/2?

Suggested by Danny Lichtblau, Wolfram Research, Inc.

I was trying to solve it analytically, but then I got tired and decided to "cheat". I figured that a very simple Perl script could give me an approximate value for p(n), by generating $N random $n-digit numbers and testing how many of them have every digit. The following code does that while remaining reasonably readable:

#!/usr/bin/perl -l ($n,$N) = @ARGV; for (1 .. $N) { @a = map { int(rand(10)) } 1 .. $n; my %h; @h{@a} = (); $g++ if keys %h == 10; } print $g/$N;

The question now is how short you can make a program that does the same. The only requirements are: 1) take two command-line arguments, $n and $N; and 2) print out the probability followed by a newline (the probability must be computed by the random method).

Generic rules for Perl Golf may be found at http://www.xs4all.nl/~thospel/golf/rules.html


In reply to Golf: Pandigital puzzle by itub

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.