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
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello I an not a java programmer,
I need know what this function does:
rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE)));
This just generate a random 8 number string? I need to make this exactly in PHP.
Thank you
Math.random() gives you a pseudorandom number less than 1 but at least 0.
This is multiplied by Integer.MAX_VALUE, which is 2147483647. The result is a number between 0 and 2147483646.
(int) converts this number to an integer.
"0." + converts this to a String, and adds "0." to the beginning of it.
Double.parseDouble converts this String back to a double. So now we again have a number between 0 and 1. But any number greater than 0 but less than 0.1 is impossible due to the way we created the String. "0.9" and "0.900" would both convert to 0.9.
1E8 * multiplies this by 1E8, which is 100000000.
And finally, Math.round rounds this off to the nearest integer.
So, in total, we have a supposedly random number somewhere between 0 and 99999999, but the final result is very skewed. Many numbers are impossible to generate with this code (numbers between 1 and 9999999 seem impossible, but 0 is still possible) and this code will generate certain numbers more often than other numbers (close to half the generated numbers will begin with 1, and more than average may end with 0), and I find it very unlikely that all of this is intentional (although it's impossible to say for sure without knowing the code's purpose.)
If you just need a random 8 digit number, there are easier, faster, and better ways. Why not just multiply that random number you got in the first step by 100000000, and round it off?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm looking into absolute function minimization problem in Java. Can anyone suggest libraries and solutions?
I downloaded apache common math3, however, could not find related functions to create linear function to minimize absolute functions.
Simply, I am looking into writing below functions in Java.
f(x) = b + | x - a | + d + | x - c | + f + | x - e |
I've edited this problem a bit for my second problem, here b, d, f are also linear functions.
b = a1x + b1
d = a2x + b2
f = a3x + b3
If you are summing N terms of the form |x - a_i|, consider the gradient as x increases from negative infinity:
At negative infinity, and for most of the left-hand number line, the gradient is -N;
As you pass the minimum of the a_i values, the gradient increases slightly, to -N+2;
As you pass the next smallest of the a_i values, the gradient increases again, to -N+4.
Each further a_i passed, the gradient increases by 2;
At positive infinity, the gradient is +N.
So, the gradient starts negative, increases in steps at each of the a_i positions, and ends up positive; you're looking for the point or range where the gradient is zero. This will occur "in the middle", i.e. at the median of the values a_i.
If there are an odd number of points, the median will be at a point. This is the case in the question: the minimum is at median(a, c, e).
If there are an even number of points, the median is between two points. In this case, the function is minimised anywhere between those points.
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 8 years ago.
Improve this question
I just have a simple question. In Java, we know that the remainder of (y%x) can't be greater than x itself. Thus, we could hypothetically set all numbers less than a certain value of x, say 100. Yet what would be the opposite of this? What if we wanted to set all numbers above a certain value, say 20, to have a range [20,100]?
I was thinking that we could subtract 20 from both sides to have the range [0,80], and then take the modulus of 80, and then add 20 to it.
Am I right in believing this?
Thanks.
The Random class includes nextInt(int) which (per the Javadoc) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. So, 0-80 is
Random rand = new Random();
int v = rand.nextInt(81);
If you want it in the ragne 20-100 that would be
int v = rand.nextInt(81) + 20;
How can the remainder be greater that the divisor ? In other words you division is not complete.
If you want to of from 20-80 you can simple do
int i = 20 + randomInteger % 80;
This will be in the range of 20 -100.
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...
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 am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)