Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by Cody Fendant (Hermit)
on Apr 11, 2018 at 06:50 UTC ( [id://1212655]=perlquestion: print w/replies, xml ) 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?
  • Download Code

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 (Monsignor) 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 (Canon) on Apr 11, 2018 at 07:13 UTC
    Hello,

    your answer is in the tartaglia's triangle aka pascal one

    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 (Canon) 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 (Patriarch) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found