Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im inputing three numbers of users choice
number 1
number 2
final number

what i want to find out is , how many combinations of number 1 and number 2 would result in final number = number1 *x + number2*y = final number.

I am using nested loops

C
int i;
int j;
long long int pocet=0; // the numbers of combination
long long int one; // first number inputed by user
long long int two; // second number inputed by user
long long int last; // final number inputed by user - wanted result
long long int one_max;
long long int two_max;
long long int add=1; //for incrementing loops
long long int reduc=1; // for decrementing loops

if((one%2==0) && (two%2==0)){
        if(one>two){
            add=two/2;
            reduc=one/2;
    }
}
one_max=last/one;
two_max=last/two;
for(i=0;i<=one_max;i+=add){
    for(j=two_max;j>=0;j-=reduc){
        long long int tmp_one=(one*i);
        long long int tmp_two=(two*j);
            if(tmp_one+tmp_two==last){
                two_max=j;
                pocet++;
        }
    }
}


if i input these numbers
one = 10;
two = 13;
final =100;
the only possible combination of first two numbers to result in final number is
10 * 10 + 13 * 0
The numbers have to be non negative
Posted
Updated 7-Nov-15 6:24am
v5
Comments
PIEBALDconsult 7-Nov-15 11:12am    
That's hard to read with it's inconsistent indenting odd words thrown in. It doesn't look like it would even compile.
Please use Improve question to format the code consistently and add more detail about what you are trying to do and why.
Member 11378302 7-Nov-15 11:25am    
i have updated question.
nv3 7-Nov-15 12:04pm    
Your statement is not true:

10 * -120 + 13 * 100 = 100

So there is obviously more than one combination of x any that fits the rule. Or you should have stated that x and y need to be non-negative.
Member 11378302 7-Nov-15 12:25pm    
indeed , i have updated the question for this info.
BillWoodruff 7-Nov-15 16:17pm    
Fix your code so it compiles.

You already posted this question in the Algorithms forum. Please post in one place only.
 
Share this answer
 
Here is code with first batch of changes. the code is changed to a function for practical reasons.
C++
long long int MyFunc (long long int one, long long int two, long long int last) {
	// one; // first number inputed by user
	// two; // second number inputed by user
	// int last; // final number inputed by user - wanted result
	
	// Question: How many (i,j) pairs (as positive integers) will verify
	// one*i+ two*j = last (constant part is on right)
	
	long long int i;
	long long int j;
	long long int pocet=0; // the numbers of combination
	
	for(i=0;i*one <=last;i++){
		// two*j = last - one*i (constant part is on right)
		// j = (last - one*i) / two (constant part is on right)
		if ((last - one*i )% two == 0) { // integer ?
			// solution
			j= (last - one*i) / two;
			pocet++;
		}
	}
	return pocet;
}

I just stick to the problem here.
I just take advantage of mathematics to change the equation as i is known. This should already improve the runtime.
 
Share this answer
 
Comments
Patrice T 7-Nov-15 16:49pm    
To downvoters: what is wrong in this answer ?
It really improve the runtime of the routine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900