Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Sorry, I hadn't studied the code well enough that you intended to find years with a unique solution, and the solution requiring six terms. A modified version of my program finds 5106 and 5328.

Also, my modified versions no longer has three digits hardcoded. You can now give the number of digits on the command line - you can also give the minimum number of terms in the unique solution, and the range of years to investigate. Unfortunally, doing it for 4 digits takes a long time. There are about 12 billion sums to consider, compared to about 14 thousand for 3 digits.

Abigail

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; GetOptions 'digits=i' => \my $digits, # Number of digits. 'min=i' => \my $min, # Minimum sum. 'max=i' => \my $max, # Maximum sum. 'terms=i' => \my $terms, # Minimum number of terms in su +m. ; $digits ||= 3; $min ||= 0; $max ||= 0x7FFFFFFF; $terms ||= 6; # # Return all permutations of a set. # sub perms; sub perms { @_ ? map {my $i = $_; map {[$_ [$i] => @$_]} perms @_ [0 .. $i - 1, $i + 1 .. $#_]} 0 .. $#_ : []; } # # Return the powerset of a set. # sub power_set; sub power_set { @_ ? do {my @r = power_set @_ [1 .. $#_]; [$_ [0]] => @r => map {[$_ [0] => @$_]} @r} : () } my %answers; sub try { local $" = " + "; foreach my $set (power_set @_) { my $sum = eval "@$set"; push @{$answers {$sum}} => $set; } } OUTER: foreach (("0" x $digits) .. ("9" x $digits)) { my @digits = split //; for (my $i = 1; $i < @digits; $i ++) { next OUTER if $digits [$i] < $digits [$i - 1] } my %seen; try grep {!$seen {$_} ++} map {$_ = join "" => @$_; s/^0*(?=\d)//; $_} perms @digit +s; } foreach my $sum (sort {$a <=> $b} keys %answers) { next unless $sum >= $min && $sum < $max; next unless @{$answers {$sum}} == 1; next unless @{$answers {$sum} [0]} >= $terms; local $" = " + "; print "$sum == @{$answers{$sum}[0]}\n"; } __END__

In reply to Re: Coming soon: Algorithm::Loops (puzzle2) by Abigail-II
in thread Coming soon: Algorithm::Loops by tye

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2021-04-13 23:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?