Big-0 notations Order of Magnitude [closed] - java

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.

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));

How to add O(N) and O(M)? [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
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)

Which equation represents quadratic time [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 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.

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

RPG Damage Calculations? [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 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

Categories

Resources