Syntactic Confectionery Delight PerlMonks

### Which Permute or other module can give me all possible subsets of 6 numbers out of 40?

by Cody Fendant (Friar)
 on Apr 11, 2018 at 06:50 UTC Need Help??
Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking through different modules and don't seem to see one which provides this.

Given a lottery in which 6 numbers are drawn from 40, how do I generate lottery tickets covering every possible winning combination?

Order is not important and obviously each number can only be drawn once.

For instance, if two numbers are drawn from three possible numbers (1, 2, 3), the combinations are:

• 1,2
• 1,3
• 2,3

Is there a module which would generate that for me with "any six numbers drawn from [1..40]"?

• Comment on Which Permute or other module can give me all possible subsets of 6 numbers out of 40?

Replies are listed 'Best First'.
Re: Which Permute or other module can give me all possible subsets of 6 numbers out of 40?
by hdb (Monsignor) on Apr 11, 2018 at 07:06 UTC
Re: Which Permute or other module can give me all possible subsets of 6 numbers out of 40?
by tybalt89 (Vicar) on Apr 11, 2018 at 10:01 UTC
```#!/usr/bin/perl

# http://perlmonks.org/?node_id=1212655

use strict;
use warnings;

my \$draw = 2;
my \$from = 3;

my \$pattern = '(2+?)' x \$draw;
(2 x \$from) =~ /^\$pattern(??{print "@+[1..\$draw]\n"})/;
Excuse my thickness, but why do we need 2 here? It works equally well with 0, 3, 4, etc., or with characters like a, b etc., but it doesn't work with 1. Why's that?

```(\$q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+\$1,++print+eval\$q,q,a,

Because the print returns 1, and we need the (??{...}) block to return something that does not match, to cause the regex engine to backtrack.

Re: Which Permute or other module can give me all possible subsets of 6 numbers out of 40?
by Discipulus (Monsignor) on Apr 11, 2018 at 07:13 UTC
Hello,

```              n!
C(n,k) =  ----------
k!(n-k)!

is the basic formula to get your answer. To get some code search for tartaglia and pascal here at the monastery or look here.

If you want a module you can look at Math::Combinatorics too

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Which Permute or other module can give me all possible subsets of 6 numbers out of 40?
by Tux (Abbot) on Apr 11, 2018 at 09:01 UTC
Re: Which Permute or other module can give me all possible subsets of 6 numbers out of 40?
by Corion (Pope) on Apr 11, 2018 at 07:10 UTC

If I understand you correctly, this is the basic example of NestedLoops from Algorithm::Loops:

```use Data::Dumper;
use Algorithm::Loops 'NestedLoops';

my \$depth = 6;
my \$range = 40;

NestedLoops( [[1..\$range], ( sub { [\$_+1..\$range] } ) x (\$depth-1)], s
+ub { print Dumper \@_})

If you want to create all those pairings in memory, NestedLoops can also give you the list instead of invoking a callback.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1212655]
Approved by hdb
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-11-20 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My code is most likely broken because:

Results (231 votes). Check out past polls.

Notices?