Reputation: 41
Suppose I have 3 vectors, L
, Y
, and R
. How can I find all possible combinations of L
, Y
, and R
that multiply to a given product?
For example, how can I find all the combinations of L
, Y
, and R
such that L * Y * R = 6
?
Upvotes: 4
Views: 74
Reputation: 101343
Here is a brute-force base R solution using expand.grid
+ subset
+ Reduce
N <- 6
v <- seq(N)[N%%seq(N)==0]
res <- subset(
res <- expand.grid(rep(list(v),3)),
Reduce("*",res)==N
)
which gives
> res
Var1 Var2 Var3
4 6 1 1
7 3 2 1
10 2 3 1
13 1 6 1
19 3 1 2
25 1 3 2
34 2 1 3
37 1 2 3
49 1 1 6
Upvotes: 1
Reputation: 518
What are possible values for your parameters? If not too many, brute-force is always an option:
L_space = seq(-10,10,0.1)
Y_space = seq(-10,10,0.1)
R_space = seq(-10,10,0.1)
for(L in L_space) for(Y in Y_space) for(R in R_space)
if(L*Y*R==6) print(paste(L,Y,R))
Upvotes: 0