Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How to determine & record all possible variations for 5 items?

by kvale (Monsignor)
on May 19, 2005 at 04:09 UTC ( [id://458510]=note: print w/replies, xml ) Need Help??


in reply to How to determine & record all possible variations for 5 items?

Here is a simple brute force enumeration for splitting 100 into 5 parts, each a multiple of 2:
my $step = 2; for (my $a1 = 0; $a1 <= 100; $a1 += $step) { for (my $a2 = 0; $a2 <= 100-$a1; $a2 += $step) { for (my $a3 = 0; $a3 <= 100-$a1-$a2; $a3 += $step) { for (my $a4 = 0; $a4 <= 100-$a1-$a2-$a3; $a4 += $step) { my $a5 = 100-$a1-$a2-$a3-$a4; last if $a5 < 0; print "$a1 $a2 $a3 $a4 $a5\n"; } } } }
If you want to get fancier, such as varying the number of parts, you may want to check out Algorithm::Loops

-Mark

  • Comment on Re: How to determine & record all possible variations for 5 items?
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found