How get the mod inverse [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The expression is: n mod m = x
I want to know the value of n given m and x
Is possible to get this value ? Is there is a java function to get that number?

It isn't possible to get this value. There are in fact multiple possibilities for a given m and x. For example, take n mod 3 = 1. We know that m is 3 and x is 1, but just knowing that, we don't know whether n is 4 or 7 or 10 or 13 or any other number that is one more than a multiple of three.

I don't think that can be done - look at a few examples
10 mod 9 = 1
19 mod 9 = 1
n could be 10, 19, 28 etc...
or
9 mod 8 = 1
17 mod 8 = 1
n could be 9, 17, 25 etc...

Related

Positive negation in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to make that after the negation operation, i.e. ~ 10 (binary: 1010), the result would not be -11 but 5, 10 = binary 1010 and after negation 0101 or 5.
Thank you for help!
The binary representation of 10 is not 1010, it's 000...0001010 (with total of 32 bits).
Therefore the negation is 111...1110101, which is -11.
If you want to get 5, you need to keep only the least significant 4 bits (and reset all the rest to 0):
int x = 10;
System.out.println (~x & 0xf);
For a more general solution, if you want to negate only the n least significant bits (where n-1 is the index of the highest 1 bit in the input number) and keep all the higher bits 0, you can use Lino's suggestion:
System.out.println (~x & ((Integer.highestOneBit(x) << 1) - 1));

Number of different possible solution to an equation in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I need to find the number of total possible different solution to an equation with more than one variable.
For example:
1x + 2y + 8z = 13
What is the total number of different combinations for the values of x, y and z?
I can't think of an algorithm to solve this. I don't need the answers printed, just the total number of different combinations. The coefficients and variables will always be positive, and the final number too.
1x + 2y + 8z = 13
Hence try (x, y,z) from (0, 0, 0) upto (13, 6, 1)
Hence at most 14*7*2 tries
Sort by the highest coefficient: z, y, x. The last variable can be inferred

Java - Why does 0 % 3 return 0? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I recently stumbled across this issue whilst programing. I wonder how this is possible, as modulo returns the remainder of the euclidean division a / b. Doesn't this mean that it would have to return 3 since returning 0 would mean that you have to perform a division by 0, which is impossible? Also, how is this handled in other programming languages? I know that modulo has very varying implementations throughout different languages, but is this a variable aspect or is it set by some mathematical statement?
Doesn't this mean that it would have to return 3 since returning 0 would mean that you have to perform a division by 0
No. You're not dividing 3 by 0, you're dividing 0 by 3, which of course yields a modulus of 0.
If a % b is the remainder of a / b, then 0 % 3 must be the remainder of 0 / 3. Since 0 / 3 = 0, there is no remainder. There is no division by zero.

Split string based on interval [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split my strings in JAVA based on a regular interval, not on regex. This is what I have to split:
1 x3.1.105.41 1 -10
2 x4.1.105.41 0 -10
3 x12.1.105.41 0 -10
4 y3.1.105.41.19 1 0
5 y4.1.105.41.21 0 0
6 y1.1.105.41.23 0 0
7 y12.1.105.41.25 0 0
I would like to seperate each column. Currently, I use the strLine.spli function
Any help would be great!
You can use substring:
String myLine = "1 x3.1.105.41 1 -10";
String column1 = myLine.substring(0, 2).trim();
String column2 = myLine.substring(2, 20).trim();
...
Or just split the lines:
String myLine = "1 x3.1.105.41 1 -10";
String[] columns = myLine.trim().split("\\s+");
which gives you in columns[0] your first value, in `columns[1]ยด your second and so on.
The second solution looks smarter to me.

can't work out answer for simple practice quiz, looked in book/online can't find answer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
int[] x = {1, 2, 3, 4};
System.out.println(x[x[3-2]]);
can some one please explain what is going on?
can't find anywhere in my text book or online for an explanation
3-2 == 1, x[1] == 2; x[2] == 3.
That is, you first evaluate the expression 3-2. Then you evaluate the expression x[1], and so on, up the "levels of nestedness".
3 - 2 = 1
x[3-2] = x[1] = 2
x[x[3-2]] = x[2] = 3
So output should be "3"
starts out 3-2 = 1
so x[1] = 2 (zero based index)
which means you then have x[2] which is 3
so.. x[x[3-2]] = x[x[1]] = x[2] = 3

Categories

Resources