[R] All the products of common factors

Fox, Gordon gfox at cas.usf.edu
Wed Feb 25 15:25:52 CET 2009

Thanks for all the suggestions!

The tricky part isn't finding the common factors -- we knew how to do
that, though not in so concise a fashion as some of these suggestions.
It was finding all their products without what I (as a recovered Fortran
programmer) would call "truly brute force." Several of these suggestions
solve the problem nicely!


P.S. The numbers involved will never be very large -- these are
dimensions of areas in which trees were sampled, in meters. They'll
always be on the order of 50-100m or so on a side.
Dr. Gordon A. Fox       Voice: (813)974-7352       Fax: (813)974-3263
Dept. of Integrative Biology ((for US mail:)SCA 110) ((for FedEx
etc:)NES 107)
Univ. of South Florida                 4202 E. Fowler Ave.
Tampa, FL 33620, USA                   http://foxlab.cas.usf.edu

"All the fun of sitting still, being quiet, writing down numbers. Yes,
science has it all." -- Principal Skinner

-----Original Message-----
From: macrakis at gmail.com [mailto:macrakis at gmail.com] On Behalf Of
Stavros Macrakis
Sent: Wednesday, February 25, 2009 2:02 AM
To: Fox, Gordon; r-help at r-project.org
Subject: Re: [R] All the products of common factors

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


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
>        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