Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: One Zero variants_without_repetition

by ohcamacj (Beadle)
on Aug 08, 2007 at 02:59 UTC ( [id://631207]=note: print w/replies, xml ) Need Help??


in reply to Re^4: One Zero variants_without_repetition
in thread One Zero variants_without_repetition

With 435 zeroes and 343 ones, you'll never live long enough to see it finish, regardless of the method used to generate the sequence. A simple way to figure out how many permutations a list will generate is with
my $num_zeros = 3; my $num_ones = 2; use Math::BigInt; sub fac { my $result = Math::BigInt->new("1"); my $n = Math::BigInt->new(shift()); while($n){ $result *= $n--; } return $result; } my $num_permutations = fac($num_ones + $num_zeros ) / (fac($num_ones) +* fac($num_zeros)); print("Number of permutations of a ($num_zeros, $num_ones) list is $nu +m_permutations\n");'
A list with 453 zeros and 343 ones will have roughly 1.962e230 different permutations -- are you sure you need all of them?

Replies are listed 'Best First'.
Re^6: One Zero variants_without_repetition (logue)
by tye (Sage) on Aug 08, 2007 at 05:58 UTC

    I like this way of computing it better, for several mostly unimportant reasons. :)

    #!/usr/bin/perl -w use strict; print countPairPermutations( @ARGV ), $/; sub countPairPermutations { my( $x, $y )= @_; ( $x, $y )= ( $y, $x ) if $y < $x; my $log= 0; for my $i ( 1 .. $x ) { $log += log( $y+$i ) - log( $i ); } my $exp= int( $log / log(10) ); my $mant= exp( $log - log(10)*$exp ); $mant= sprintf "%.*f", 6 < $exp ? 3 : 8, $mant; $mant .= $exp ? "e" . $exp : ""; return $mant if 6 < $exp; return 0 + $mant; }

    My code appears to agree with your code (though I didn't count the number of digits output by your code) but they both disagree with your node, reporting that there are 5.807e234 different permutations (not 1.962e230).

    Update: Or, using my prototype module:

    #!/usr/bin/perl -w use strict; require Math::BigPositiveOkayPrecision; print countPairPermutations( @ARGV ), $/; sub countPairPermutations { my( $x, $y )= @_; ( $x, $y )= ( $y, $x ) if $y < $x; my $p= Math::BigPositiveOkayPrecision->new( 1 ); for my $i ( 1 .. $x ) { $p= $p * ( $y+$i ) / $i; } return $p; }

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://631207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found