nev*_*int 2 algorithm r combinatorics
我怎样才能找到两个数字的所有组合,使得两个值的总和等于tot_max <= 16(最大,允许更少)并且两者之间的比率为rat=0.5。
这样的例子是:
V1 V2 ratio(V1/V2) sum
1 2 0.5 3. OK
2 4 0.5 6. OK
3 6 0.5 9. OK
4 8 0.5 12. OK
8 16 0.5 24. NOT OK (over 16)
Run Code Online (Sandbox Code Playgroud)
这两个数字(V1 和 V2)应为大于 0 的整数。
如何创建 R 代码以便它可以容纳tot_max和的不同值rat?
这是一个数学问题,不一定是编程问题
你有x + y < 16和x/y = 0.5。x, y > 0有了这三个两个条件,做代数就会得到:1 <= x <= 5和y = 2x。
所以:
x <- seq(5)
y <- 2*x
data.frame(x, y, sum = x+y, ratio = x/y)
x y sum ratio
1 1 2 3 0.5
2 2 4 6 0.5
3 3 6 9 0.5
4 4 8 12 0.5
5 5 10 15 0.5
Run Code Online (Sandbox Code Playgroud)