Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of solutions for 2 related equations

How do I find the number of solutions for

s = a+b
x = a^b

When s and x are given, and ^ means xor?

So for like (0,0) or (31,31) or (15,10)?

I've tried converting x into a binary string but after that I'm unsure where to go with it.

like image 972
user2124726 Avatar asked Apr 12 '15 16:04

user2124726


People also ask

How can you determine the number of solutions for an equation?

key idea. If solving an equation yields a statement that is false, like 4 = 3, then the equation has no solution. If solving an equation yields a statement that is true for a single value for the variable, like x = 3, then the equation has one solution.

How many solutions does 2 equations have?

An equation can have infinitely many solutions when it should satisfy some conditions. The system of an equation has infinitely many solutions when the lines are coincident, and they have the same y-intercept. If the two lines have the same y-intercept and the slope, they are actually in the same exact line.

How many solutions exist to a relation in 2 variables?

Thus, there are an infinite number of solutions. Another type of system of linear equations is an inconsistent system, which is one in which the equations represent two parallel lines. The lines have the same slope and different y-intercepts.

How to find the number of solutions in a system of equations?

How to Find the Number of Solutions in a System of Equations? A system of linear equations usually has a single solution, but sometimes it can have no solution (parallel lines) or infinite solutions (same line). A linear equation in two variables is an equation of the form \ (ax + by + c = 0\) where \ (a, b, c ∈ R\), \ (a\), and \ (b ≠ 0\).

How many solutions are there if (A1/A2) = (B1/B2)?

If (a 1 /a 2) = (b 1 /b 2) = (c 1 /c 2 ), then there will be infinitely many solutions. The lines will coincide. This type of equation is called a dependent pair of linear equations in two variables

How do you know if two equations have the same solution?

The lines may intersect each other at a single point. As a result, the pair of equations has a unique solution (consistent pair of equations). The lines may be parallel to each other. As a result, the equations have no solution (inconsistent pair of equations).

How do you find the unique solution of a linear equation?

Consider the pair of linear equations in two variables x and y. Here a 1, b 1, c 1, a 2, b 2, c 2 are all real numbers. 1. If (a 1 /a 2) ≠ (b 1 /b 2 ), then there will be a unique solution.


2 Answers

The method solution returns null if there are no solutions. If there is a solution, it returns a (for just one solution). You can get b by doing s - a or x ^ a.

If a solution exists, the total number of solutions (in long) is 2 to the power of Long.bitCount(x).

For example, the solution found for s = 24, x = 6 is a = 9, b = 15. In binary:

 9 = 1001
15 = 1111

These numbers differ in 2 positions, so there are Math.pow(2, 2) = 4 solutions in total. You can get all possible solutions by interchanging the bits of a with the corresponding bits of b for some or all of these positions.

This gives 3 further solutions.

11 = 1011     13 = 1101     15 = 1111
13 = 1101     11 = 1011      9 = 1001

Here is the code:

public static Long solution(long s, long x) {
    return recursive(s, x, false);
}

private static Long recursive(long s, long x, boolean carry) {
    boolean s1 = (s & 1) == 1;
    boolean x1 = (x & 1) == 1;
    if ((s1 == x1) == carry)
        return null;
    if ((s == 0 || s == -1) && (x == 0 || x == -1))
        return s;
    Long a;
    if (x1)
        return (a = recursive(s >> 1, x >> 1, carry)) == null ? null : a << 1;
    if ((a = recursive(s >> 1, x >> 1, false)) != null)
        return a << 1;
    if ((a = recursive(s >> 1, x >> 1, true)) != null)
        return 1 + (a << 1);
    return null;
}

I decided against writing a method to return a HashSet of all solutions, as these sets would, in some cases, be massive. However, you could write a method for generating all possible solutions, without storing them all in memory at once. See, for example, Generating all binary numbers based on the pattern

like image 55
Paul Boddington Avatar answered Oct 04 '22 01:10

Paul Boddington


Let's denote by v_j the j-th bit of value v with j=0 being the least significant bit.

The key observation is that the arithmetic sum a+b can be expressed in terms of xor operation a ^ b and the carry bits of the addition. We have

s_j = a_j ^ b_j ^ c_j = x ^ c_j

where c_j is the carry bit added to j-th position. To figure out what happens with carry bits, notice that

c_0 = 0
c_1 = a_0 & b_0   (so c_1 is one when both a_0 and b_0 are one)
c_j = 1 if and only if at least two of a_j, b_j, c_(j-1) are one.

The last condition is essentially saying that

c_j = Majority(a_j, b_j, c_(j-1)) = a_j & b_j ^ a_j & c_(j-1) ^ b_j & c_(j-1)

Having both a + b and a ^ b you can determine the bits c_j of the carry and from that you should be able to deduce the formula for the number of solutions for each a_j, b_j depending on the values of c_j and c_(j-1).

like image 20
Krystian Avatar answered Oct 04 '22 02:10

Krystian