Say I have
a + 3b + 4c +2d =40;
how can I solve a,b,c and d. I'm not sure where to start, I am coding this using c language. I know that one solution is a=9 b=3 c=2 d=7.
I forgot to add that the domain for a,b,c,d is 0-29 inclusive.
Say I have
a + 3b + 4c +2d =40;
how can I solve a,b,c and d. I'm not sure where to start, I am coding this using c language. I know that one solution is a=9 b=3 c=2 d=7.
I forgot to add that the domain for a,b,c,d is 0-29 inclusive.
This problem is equivalent to kind of coin change problem - get a sum with limited number (here 29 max) of coins with some nominals (here 1,2,3,4)
The simplest way to make all changes is recursive generation.
makesum(coinlist, currentsum, resultlist)
if currensum < 0
return
if currensum = 0
print resultlist
for coin in coinlist
makesum(coinlist - coin, currentsum - coinvalue, resultlist + coin)
For specific case - small fixed list of nominals - you can just make 4 nested loops
Also dynamic programming approach exists - fill a table[0..sum] with possible combinations (for getting all possible combinations DP is not faster)
There are infinite solutions to this equation, since this represents a plane in some 4d space and all points on that plane are valid solutions.
In order to have a unique solution, you need at least 4 such different equations, but then it’s still not guaranteed that you will get a solution for those set of equations after that.
Since the range of valid numbers are small (0-29) you can use brute force, i.e. 4 for-loops, and print all solutions:
#include <stdio.h>
int main(void) {
for (int a=0; a<30; ++a)
for (int b=0; b<30; ++b)
for (int c=0; c<30; ++c)
for (int d=0; d<30; ++d)
if (a + 3*b + 4*c + 2*d == 40)
printf("Solution: a=%d b=%d c=%d d=%d\n", a, b, c, d);
return 0;
}
Output:
Solution: a=0 b=0 c=0 d=20
Solution: a=0 b=0 c=1 d=18
Solution: a=0 b=0 c=2 d=16
Solution: a=0 b=0 c=3 d=14
Solution: a=0 b=0 c=4 d=12
Solution: a=0 b=0 c=5 d=10
Solution: a=0 b=0 c=6 d=8
Solution: a=0 b=0 c=7 d=6
Solution: a=0 b=0 c=8 d=4
Solution: a=0 b=0 c=9 d=2
Solution: a=0 b=0 c=10 d=0
Solution: a=0 b=2 c=0 d=17
Solution: a=0 b=2 c=1 d=15
Solution: a=0 b=2 c=2 d=13
. . .
<many more solutions>
. . .
If you want an answer for 1 the equation is three multiplyed by y = three divided by three =1 and y=1 so the answer is 3 multiplyed bt 1= 3