[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Brief Coding Methods



On Mon, Aug 23, 1999 at 04:10:09PM +0100, Alistair.McGlinchy@marks-and-spencer.com wrote:
> 
> [... Seeing as
> all seems quiet, I'll post it now. My apologies if this is an general issue
> you people have done to death ]

Permutation seems to be a hot topic around here...


> The weekly circular contained a little puzzle. Find a 4 digit number ABCD
> and a five digit number EFGHI such that:
> 	 ABCD / EFGHI = 1 / 66 
> such that numbers 1 through 9 are all represented as digits in ABCD and
> EFGHI. 
> 
> So I'd like
> to ask you if you can help improve this. The "rules" appear to be that it
> must print the answer by some method that could allow the "66" constant to
> be replaced by any other number to yield a similar result (eg "1452/98736 =
> 1/68"). 
> 
> Bonus style points are in order if your code deduces that no such solution
> exists (eg "67"). 

Ooh, I live this sort of thing!

My solution here isn't particularly short, but I think it's fun, for
certain definitions of "fun".
It prints out all solutions for all values of the parameter.


sub chooseN(&@);
sub chooseN(&@) {
  my( $sub, $n, @vec ) = @_;
  $n 
    ? map {
        chooseN {
          $sub->( $vec[$_], @_ )
        } $n-1, @vec[$_+1..$#vec]
      } 0 .. @vec-$n
    : $sub->()
}

for my $p ( 2 .. 80 ) {
  print chooseN {
    my $n = join '', @_;
    my $m = $n * $p;
    local $_ = "$n$m"; 
    /0/ || !/\d{9}/ || /(\d).*\1/
      ? ()
      : "$n / $m = 1 / $p\n"
  } 4, 1 .. 9
}



-- 
John Porter


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe