calculating number combinations in perl

I have a small problem. I am working on a probability solution.

I have quasi-randomly generated data sets, from the value of 1 to 999. These values are the end result of another mathematical process.

The data comes out in groups of seven, eight and ten.

We have a mid range of 501-550.

If I could get help on the seven group problem, Im sure I could find my way with the other two groups when I can see the code structure.

I need to build a database (which could be a text file if it is big enough) of all possible value combinations, so out of the 50 numbers, from 501 to 550.

So the first possible combination would be

Then the next possible combination would be

And the combinations would tick up one at a time until
544,545,546,547,548,549,550, which would be the final one.

I expect about 10 million results.

I need each individual result output to a text file / word pad one line at a time, delineated by a comma.

Could you tell me perl code which would do this ?

Thankyou very much
Europa MacDonaldChief slayer of dragonsAsked:
Who is Participating?
Carl BohmanConnect With a Mentor Commented:
If you don't have Math::Combinatorics installed and can't install it for some reason, here's a code version.

For either my solution or Adam's, just redirect the output to a text file using the greater than symbol: > filename.txt
my $count = 3;
my $low = 501;
my $high = 550;
sub cmb
  my ($rem, $used, $text, $min) = @_;
  for my $n ($min..($high - $rem))
    next if $used->{$n};
    if ($rem)
      $used->{$n} = 1;
      cmb($rem, $used, "$text$n,", $n + 1);
      $used->{$n} = 0;
    } else
      print "$text$n\n"
my %h;
cmb($count, \%h, "", $low);

Open in new window


use Math::Combinatorics;
my $combin = Math::Combinatorics->new(count => 7, data => [501..550]);
while(my @combo = $combin->next_combination){
	print join(',', @combo)."\n";

Open in new window

Europa MacDonaldChief slayer of dragonsAuthor Commented:
thankyou Adam

would this print out to a text file ?
Europa MacDonaldChief slayer of dragonsAuthor Commented:

where would I put > filename.txt


Europa MacDonaldChief slayer of dragonsAuthor Commented:
worked out the file thing myself
thankyou very much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.