Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by jdporter (Paladin)
on May 19, 2005 at 04:10 UTC ( [id://458511]=note: print w/replies, xml ) Need Help??


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

I tend to prefer a recursive approach for things like this; makes it easier to generalize. The following is kind of brute force, but I think it's correct:
gen( 5, 50, 0, sub { print "@_\n" } ); sub gen { my( $cols, $max, $used, $found, @vec ) = @_; return $found->( @vec, $max-$used ) if $cols == 1; gen( $cols-1, $max, $used+$_, $found, @vec, $_ ) for 0 .. $max-$used; }
Update: I passed 50 for the 'max' value here because it gives the result you want when going to 100 by 2's. Just multiply every number in every cell of the result by 2. Similarly, if you want to go to 100 by 5's, pass 20, and multiply all the numbers in the result by 5.
  • 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://458511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found