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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

In the spirit of TIMTOLanguageTDI, here's a way to do it in Haskell:

partition :: Integer -> Integer -> [[Integer]] partition total 1 = [[total]] partition total numparts = concat $ map (prepend) [total, (total - 1) .. 0] where prepend x = map (x:) (subpart x) subpart x = partition (total - x) (numparts - 1) -- then: partition 100 5
Note that this will take a while, as there are over 4.5 million solutions. This doesn't take into account the "only multiples of 2 or 5" criteria, but it's easy to change it so that it does:
partition :: Integer -> Integer -> [[Integer]] partition total 1 = [[total]] partition total numparts = concat $ map (prepend) numlist where prepend x = map (x:) (subpart x) subpart x = partition (total - x) (numparts - 1) numlist = filter twoorfive [total, (total - 1) .. 0] twoorfive x = x `mod` 2 == 0 || x `mod` 5 == 0
And this gives about 600k solutions. Much more manageable.

Another way to do it would be to create all possible lists of the desired length (5 in this case), with each element in a given range (0..100 here) and filter out the ones that don't sum to 100. I tried this approach as a mind exercise (you probably wouldn't want to use it because it would be very ineffecient). I got it to work, but only by hardcoding the number of elements to five, using a list comprehension. I know this is a Perl board, but anyone familiar with Haskell know of an easy and intuitive way to do something like:

allLists :: Integer -> Integer -> [[Integer]] allLists n len = ?
And allLists would return all lists of length len where each element is in the range 0..n?


In reply to Re: How to determine & record all possible variations for 5 items? (Haskell) by kelan
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 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? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-10-01 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (4 votes). Check out past polls.

    Notices?