Skip to content
Back to formatted view

Raw Message

Message-ID: <8b356f880902242056t6b0ee641q7c384581286c5ed7@mail.gmail.com>
Date: 2009-02-25T04:56:24Z
From: Stavros Macrakis
Subject: All the products of common factors
In-Reply-To: <8b356f880902241455x5b6ecb84t9aece7e180e5ea19@mail.gmail.com>

"L'esprit de l'escalier" strikes again....

An even simpler statement of your original problem:

      Find the factors that A and B have in common.

If A and B are fairly small (< 1e7, say), a very direct approach is:

       which(  ! (A %% 1:min(A,B)) &  !(B %% 1:min(A,B)) )

Is this "brute force"?  Well, I suppose, but it is simple and direct
and fast enough for A=B=1e7 (5 sec).  It doesn't involve factorization
into prime factors, GCDs, or combinations.

If your goal is concision and not clarity or speed, you can do even better:

       which(  !(A %% 1:B & B %% 1:A) )

which is still practical (though it gives a warning).

How big do your A and B get, and how many different A's and B's do you
need to run this calculation for?

               -s