On 22-02-2012, at 13:49, Silvano wrote: > Hi, > > I have the following equation: > > x1 + x2 + x3 - 2(x4 + x5 + x6) + 3(x7) = N > > each x_i can take any value: 1, 2, 3, 5, 6, 10, 15 or 30 and > each one is different from each other. > > Which combination of values ??in the formula which leads to the smallest > value of N? > > How can I program this situation?
I hope this isn't homework. library(gtools) val <- c(1,2,3,5,6,10,15,30) x.vals <- permutations(n=length(val),r=7,v=val) f <- function(x) x[1]+x[2]+x[3]-2*(x[4]+x[5]+x[6])+3*x[7] fun.vals <- apply(x.vals,1, function(z) f(z)) minidx <- which.min(fun.vals) fun.vals[minidx] x.vals[minidx,] f(x.vals[minidx,]) Berend Hasselman ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.