Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The following is an example of a branch and bound approach.

NB: Just a hack as a proof of concept, without taking advantage of the optimization ideas I've already described.

Nevertheless it calculates:

====== Calculating base 12 *** Results 18 took: 0 sec ====== Calculating base 14 *** Results 136 took: 0 sec ====== Calculating base 16 *** Results 188 took: 1 sec ====== Calculating base 18 *** Results 7478 took: 5 sec ====== Calculating base 20 *** Results 41984 took: 49 sec

use strict; use warnings; use Data::Dump qw/pp dd/; use feature 'say'; my $base= 16; my $verbose = 0 ; my @digits = 0 .. $base-1; my %allowed; @allowed{@digits} = (1)x@digits; delete $allowed{0}; #pp \%allowed; our $i; our $carry; our $level; our @factor; our @product; our @result; warn "====== Calculating base $base\n"; my $start =time; for $i ( reverse 2 .. $base -1 ) { delete $allowed{$i} ; warn "=== \$i = $i\n" if $verbose; $carry = 0; $level=0; branch(); $allowed{$i}=1; } say "*** Results ", scalar @result; say "took: ", time-$start , " sec"; sub branch { #say pp \%allowed unless $level; local $level = $level+1; for my $f ( sort keys %allowed ) { my $p = $i *$f + $carry; my $digit = $p % $base; local $carry = int ($p / $base); next if $digit == $f; next unless $allowed{$digit}; unshift @factor,$f; unshift @product,$digit ; warn " " x $level, "Level$level <$f>: $i * @factor = @product with <$carry $d +igit> remain ", (sort keys %allowed) , "\n" if $verbose > 2; delete $allowed{$f}; delete $allowed{$digit}; if (keys %allowed) { branch() } elsif (! $carry) { warn "-" x $level, "RESULT Level$level: $i * @factor = @ +product with <$carry $digit> remain ", (sort keys %allowed) , "\n" i +f $verbose >1; push @result, [ $i, [@factor], [@product]]; } $allowed{$f} = 1 ; $allowed{$digit} = 1 ; shift @factor; shift @product; } }

Update: code reformat.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice


In reply to Re: Efficient enumeration of pandigital fractions by LanX
in thread Efficient enumeration of pandigital fractions by kikuchiyo

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-16 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found