Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
BrowserUk,
The order and pairings you choose to cancel out affect the result. This may prevent optimizations that seek to cancel out terms in a different order (such as largest terms first). For instance in your example, it took 6 cancelings to get the resut. Here is another possibility that doesn't result in prime factorization but only takes 3 pairings:
# After duplicates have been removed # @a = ( 20, 33, 60 ); # @b = ( 2, 5, 12, 16, 23 ); 60 / 12 = 5 / 1 # @a = ( 20, 33, 5 ); # @b = ( 2, 5, 16, 23 ); 20 / 5 = 4 / 1 # @a = ( 4, 33, 5 ); # @b = ( 2, 16, 23 ); 16 / 4 = 4 / 1 # @a = ( 33, 5 ); # @b = ( 2, 4, 23 );

My algorithm doesn't require prime factorization (just GCD).

#!/usr/bin/perl use strict; use warnings; use Inline 'C'; my (%set_a, %set_b); ++$set_a{$_} for 10, 20, 33, 45, 60; ++$set_b{$_} for 2, 5, 10, 12, 16, 23, 45; cancel_out(); # Assume %set_a and %set_b at package scope print "$_\t$set_a{$_}\n" for keys %set_a; print "\n\n\n"; print "$_\t$set_b{$_}\n" for keys %set_b; sub cancel_out { my $finished; while ( ! $finished ) { $finished = 1; for ( keys %set_a ) { if ( exists $set_b{$_} ) { my $res = $set_a{$_} <=> $set_b{$_}; if ( ! $res ) { delete $set_a{$_}, delete $set_b{$_} } elsif ( $res < 0 ) { $set_b{$_} -= delete($set_a{$_}); } else { $set_a{$_} -= delete($set_b{$_}); } } next if ! exists $set_a{$_}; my $m = $_; for my $n ( keys %set_b ) { my $gcd = gcd($m, $n); if ($gcd > 1 ) { ++$set_a{$m / $gcd} if $m != $gcd; ++$set_b{$n / $gcd} if $n != $gcd; ! --$set_a{$m} && delete $set_a{$m}; ! --$set_b{$n} && delete $set_b{$n}; $finished = 0, last; } } } } } __END__ __C__ /* Implementation of Euclid's Algorithm by fizbin */ int gcd(int m, int n) { while( 1 ) { if (n==0) {return m;} m %= n; if (m==0) {return n;} n %= m; } }

It doesn't produce the output you wanted so I didn't bother trying to optimize it WRT choosing pairings. With the lack of optimizations it is likely not that efficient - but it was fun.

Cheers - L~R


In reply to Re: Algorithm for cancelling common factors between two lists of multiplicands by Limbic~Region
in thread Algorithm for cancelling common factors between two lists of multiplicands by BrowserUk

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 pondering the Monastery: (4)
    As of 2019-10-18 21:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?