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 4 years ago.
Improve this question
I believe it is 3n+5n^2+1 but I am not 100 percent sure. If I am wrong can someone explain to me why?
3n + 5n^2 + 1 | applying Big-O notation for the single terms
= O(3n) + O(5n^2) + O(1) | leaving out constant multipliers
= O(n) + O(n^2) + O(1) | taking the one with the maximum power
= O(n^2)
=> quadratic time
So you are right, the first option is the quadratic one.
If it was 3n + 5n^(2 + 1), then it would be qubical due to 5^(2 + 1) = 5^3.
Related
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
Say I have two operations that are O(N), so I represent one as O(N) and the other as, say, O(M). If I added them together, what would it be? There's no telling which one is the greater of the two.
Also, is O(N) + O(M) the same as O(N + M)? If so, why?
If your input is N,M, then yes: O(N) + O(M) = O(N+M).
Why is that?
O(N) + O(M) is bounded by O(N+M):
O(N) + O(M) is a*N + b*M operations [where a, b are constants].
a*N + b*M <= (a+b)*N + (a+b)*M = (a+b)*(N+M) <= c*(N+M) [where c = a + b].
c is constant so c*(N+M) is O(N+M).
O(N+M) is bounded by O(N) +O(M):
O(N+M) = e*(N+M) = e*N + e*M [for some constant e],
which is O(N) + O(M)
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 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
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 8 years ago.
Improve this question
I am making a small RPG using Jcreator.
I give whatever character you pick damage at the start, we will take the Swordsman as an example.
I gave him 10 Damage and for the hitting damage I made the int Hit.
My problem is that this random number is not working well for damage as it is giving me damage under the actual attack range.
Hit = 1 + (int) ((Math.random() * (Damage - 1)) +1);
You need to always know the bounds of your random number generator.
In pseudocode, to generate a number from a random space with a minimum and maximum,
Result = Minimum + (Maximum - Minimum) * (Random() - RandMin) / (RandMax - RandMin)
Try this:
hit = (int)(Math.random() * range) + min;
where range = the max value you want minus the min value you want, i.e.,
int range = (max - min) + 1;
Something like the following should work, and give you a number in the range of 5 above or below your damage:
randomNum = Damage-5 + (int)(Math.random()*Damage+1);
I tested it and it seems to have worked for me, didn't get a number below 5 or greater than 15
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm doing some homework and just want to make sure I am on the right track.
The question is:
Describe the order of magnitude of each of the following functions using Big-O notation.
1) N^2 + 3N = my answer O(N^2)
2) 3N^2 + N = my answer O(N^2)
3) N^5 + 100N^3 + 245 = my answer O(N^5)
4) 3Nlog2N + N^2 – my answer O(N^2)
5) 1 + N + N^2 + N^3 + N^4 = my answer O(N^4)
6) (N * (N – 1)) / 2 - my answer O(N^2)
Am I doing this right? Any suggestions?
The Big-O Notation's Order of magnitude is the one with the highest power (because they are, in most cases, the most computationally expensive function). So, you will have to see, in your formula, which function is most computationally expensive to do.
The first 2 is correct. The other 3, well.... ;)
Update: Question 1, 2, 3 and 6 are correct.