Reputation: 626
How it would be possible to find/guess the combination of N
numbers such as 5 or 7 or whatever
that gives a final number R
?
For example, determine N = 5
and R = 15
The one possible result/guess that the 5
numbers in which their summation give 15
would be {1,2,3,4,5}
Upvotes: 1
Views: 710
Reputation: 9085
To get n floating point numbers that total a target r:
-edit (thanks @ruakh) -
Upvotes: 3
Reputation: 331
Python code
import random
N = 5
R = 7
result = random.sample(range(0, int(R)), N-1)
result.append(R - sum(result))
Upvotes: 0
Reputation: 96
This can be solved by backtracking, this is really slow, because it looks for all combinations, but if you only want one combination use K-1 0' and N, this sum = N
n = 15
k = 2
array = [0] * k
def comb (len, sum):
if len == k - 1:
array[len] = sum
print(array)
return
for i in range(0, n + 1):
if sum - i >= 0:
array[len] = i
comb(len + 1, sum - i)
comb(0, n)
Upvotes: 0