I’m new to programming so I’m sorry in phrasing if I’m not asking this question correctly.
I have the following code:
int sum = 100;
int a1 = 20;
int a2 = 5;
int a3 = 10;
for (int i = 0; i * a1 <= sum; i++) {
for (int j = 0; i * a1 + j * a2 <= sum; j++) {
for (int k = 0; i * a1 + j * a2 + k * a3 <= sum; k++) {
if (i * a1 + j * a2 + k * a3 == sum) {
System.out.println(i + "," + j + "," + k);
}
}
}
}
basically what it does is tell me the different combinations of a1, a2, and a3 that equal the above sum (in this case 100). This works fine but I’m trying to apply it to a larger dataset now and I’m not sure how to do without manually programming the for loops or knowing in advanced how many variables I’ll have(could be anywhere from 10 to 6000). I basically have a sql query that loads that data from an array.
Is there a way in Java OR python (I’m learning both) to automatically create nested for and if loops?
Thanks so much in advance.
Recursion.
This is what it sounds like you are trying to solve:
and to call for your example you’d use: