#!/usr/bin/perl -w use strict; # ----------------------------------------------- # Here's some golf-ish code that loops through the # given combinations, as well as a clearer rendition. # It uses a hash to eliminate duplicates, and # strings rather than an array for each combo. # I expect it can be done in many fewer strokes, # but I had fun playing around with it, anyway, # and this four liner looked obfuscated enough. # The output is listed at the end. my($N,$M)=(8,6);my%c;r($N,join('',1..$N));sub r{my($n,$s)=@_;if($n==$M){$c{$s}++unless$c{$s};}else {for(1..$N){$t=~s/$_/#/,r($n-1,$t)if(our$t=$s)=~/$_/;}}} print join($/,sort keys %c),"$/Total=",scalar(keys %c),$/; _END__ # A clearer version of the same thing. my ($N,$M) = (8,6); my %combos = (); recursive($N,join('',1..$N)); sub recursive { my ($n,$combo) = @_; if ($n==$M){ $combos{$combo}++ unless $combos{$combo}; } else { for my $i (1..$N){ if ( (my $tmp=$combo) =~ /$i/ ){ $tmp=~s/$i/#/; recursive($n-1,$tmp); } } } } print "-- Combinations of $N things with ", $N-$M, " crossed out. --\n", join("\n", sort keys %combos), "\n-- Total number is ", scalar(keys %combos), ". --\n\n"; # -------- output of the short one ------------- ##345678 #2#45678 #23#5678 #234#678 #2345#78 #23456#8 #234567# 1##45678 1#3#5678 1#34#678 1#345#78 1#3456#8 1#34567# 12##5678 12#4#678 12#45#78 12#456#8 12#4567# 123##678 123#5#78 123#56#8 123#567# 1234##78 1234#6#8 1234#67# 12345##8 12345#7# 123456## Total=28

In reply to Re: Lottery combinations golf by barrachois
in thread Lottery combinations golf by Cody Pendant

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.