Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
could you please specify what you mean by
I'd ideally like to be able to chose to only allow increments of 2 or +5
if you allow both increments at the same time, then any value after 3 ( = 5 * 2 - 5 - 2 ) is representable as a sum of 2's and 5's, so you might as well go the brute force way, with (0..100) without 1 and 3, cut up to current sum ( sorry, unclear here - basically, use, jdporter's solution, with generate_solutions(1, 100 ...
buth with additional checks, ie,
return if ($i == 1 || $i == 3)
if you only want to allow 1 type of increment, say 5, then you might as well generate all partitions of 20 and multiply them by 5. The above solutions either give you only one increment, or, as in Ovid's allow only one of the two to be true, ie, 7 won't be a valid element..

Rethinking further the part about both increments allowed. What about starting with an array (100,0,0,0,0), and then, from the right hand side, try to move either a 2 or 5 to the right, with backtracking recursion, printing any time you succeed without making a negative number. Hmm, but that would make it possible to shift a 2 from a 5, with a remaining 3, which is bad...

Well, ok, I'll stop now.. the above idea, with just shifting 1's, should easily generate all unrestricted partitions of a number in a lexicographic order, so if you only needed 1 increment, it should suffice.. You can probably write the actual code for it faster than I.. in the meantime, I'll start thinking on generating weird restricted partitions...


In reply to Re: How to determine & record all possible variations for 5 items? by ivancho
in thread How to determine & record all possible variations for 5 items? by Stenyj

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found