'Partition problem- all possible combinations that will modulo zero or +/- a parameter with variable inputs
Math/spacial problem to solve here with a few variables.
I want all possible combinations based on the following variables:
User inputs an overall length
User indicates amount of different object lengths
User also declares each of the different object lengths (each must be unique)
User indicates maximum leftover spacing (if zero than modulus 0 of course)
Example:
Overall length 20.5 (double)
Leftover deviation 0
Unique objects 4 (integer)
Object 1 length: 3 (double)
Object 2 length: 5.5 (double)
Object 3 length: 1.5 (double)
Object 4 length: 7 (double)
This helps but does not do nearly what I need. How can this be programatically adjustable based on the unique values presented as parameters?
As far as I've gotten with set combination values:
void main() {
void example(int target) {
for (int a=0;a<=target;a++) {
for (int b=a;b<=target-a;b++) {
for (int c=b;c<=target-(a+b);c++) {
int d = target-(a+b+c);
if(d>=c) {
print('$a, $b, $c, $d');
}
}
}
}
}
example(20);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
