[R] All the products of common factors

Stavros Macrakis macrakis at alum.mit.edu
Wed Feb 25 08:01:50 CET 2009


Argh!  The second (concise) version should have |, not & !!!

          -s

On 2/24/09, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> "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
>




More information about the R-help mailing list