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
hi to all
I want to know how to find the solution for this problem. consider in a Jar there are 10 papers numbered 1 to 10. we have to take 2 papers from that. what is the probability that 2 numbers are consecutive numbers.
It's a probability to choose one of 9 consecutive pairs from C(10, 2) possible pairs. Thus its 9 / (10*9 / 2) = 1 /5
The probability of getting 2-8 is 8/10. Once we have a given number in that range, the probability of getting one of the two adjacent numbers is 2/9. 8/10 * 2/9 = 16/90.
The probability of getting either a 1 or a 10 is 2/10. If we have one of those two endpoint numbers, the probability of getting the one adjacent number is 1/9. 2/10 * 1/9 = 2/90.
Adding these together, we have a total probability of 18/90 or 1/5.
Your first choice has ten possible numbers. 8 of those 10 will have two consecutive numbers, while the other two only have 1. Your second choice will be out of nine numbers. So
(8/10 * 2/9) + (2/10 * 1/9) = 9/45 = 1/5
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given array A[] and B[], to find how frequently each possible sums A[i] + B[j] appears we can use FFT
For example
A = [1,2,3], B = [2,4].
The sum 3 can be obtained in 1 way, sum 4 : 1 way, sum 5 : 2 ways, sum 6 : 1 way, sum 7 : 1 way
The way we can do this is to construct two polynomials, P and Q with their power corresponding to the element of the array. And apply the regular FFT.
So is there an efficient way of backtracking the numbers that forms the above. To elaborate, we know 3 can be formed in 1 way. But how do we know which two numbers form it?
One way to do it would be the classic two sum algorithm, i.e given an array and a target sum. Find the pairs that create the sum. Which is O(n)
Given we can have N different targets, the resulting algorithm is O(n^2). But I want to keep it under O(nlogn).
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 came across the following question: What is the smallest number n by which the given number x must be divided to make it into a perfect square?
n = find_number ( x )
I know the standard way to do this to find the prime factors of x and multiple by what is necessary to have perfect squares in the prime factors, but I saw this answer:
The number is 1/x so x/1/x= x^2
Not sure if that it completely wrong, or is a genius and direct solution
Thanks
no thats not correct ...
for 2 you say its 1/2 then 2 / (1/2) is 4 which is perfect square cool
but what about 1/8 then 2 /(1/8) is 16 which is perfect square
and 1/8 < 1/2 so thats one contradiction you need to know that thats not correct
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
How to check if a number is a power of 5?
I could think of below algorithm. Is there way to improve it? Any mathematical trick?
First check if last digit is 5.
If last digit is 5; divide it by 5.
If result of division is 1, then number is power of 5.
Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).
You don't need to look at individual digits, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer
if (pow(5, n) == x) // test to see whether x == 5^n
// x is a power of 5
LIVE DEMO
There are only few power of five that fit in int/long range, so you just need to generate all of them and check one by one (less than 60 numbers), using a HashSet will have O(1) time complexity
Successive division until you reach the number undivided by 5 and check whether the result is equal to 1, isn't bad solution. It take log_5(n) operations, so it's O(lg n), it's very good time. For 9094947017729282379150390625 it's only 40 operations!
What I would do is first create an array of numbers that are powers of 5. You could use a range, and then say, for each value in the range, take that value to the fifth power, and push the new value into the array.
You would then find if n, the number you are looking for is included in the array.
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 9 years ago.
Improve this question
I am trying to figure out a way to figure out how many times an even number can be divided by two before it becomes odd. I.E. 100/2 = 50 => 50/2 = 25. So 100 can be divided by two twice. before it becomes odd. I am looking for all even numbers that are NOT powers of two.
That's two questions.
I am trying to figure out a way to figure out how many times an even number can be divided by two before it becomes odd. I.E. 100/2 = 50 => 50/2 = 25. So 100 can be divided by two twice. before it becomes odd.
Convert the number binary and count the 0s before the first 1. Each time you dive by 2 you loose a zero and when a 1 is in the "first" position you've got an odd number.
I am looking for all even numbers that are NOT powers of two.
Again, looking at the binary, powers of 2 have only one bit set and odd numbers have the 1 bit set. So anything with multiple bits set AND not the 1 bit is your answer.
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 7 years ago.
Improve this question
Hi I came across this question from my friend.
Give me a generalised formula to find how many tennis matches (singles) are required for n players ?
Example : if the number of players are 16 then
first : we need 8 mataches (for 16 players) , here 8 players will be eliminated and 8 players will be there
secode : we need 4 matches (for 8 players) , here again 4 players will be eliminated and 4 will be remaining
third :
we need 2 matches (for 4 players) , here again 2 players will be eliminated and 2
will be remaining
Final :
we need 1 macth to decide a winner among the 2 players
so totally 15 matches are required.
I need a generalised formula to find , such that if I give the value n I should get the number of matches required to find the winner
n may be odd or even
For elimination game, the number of matches is always n-1, because one player will be eliminated after one game and n-1 players have to be eliminated in total.
As eventually every player but 1 (the champion) has to lost his match (and every player can lost only in 1 match) then the number of matches required is n-1
n-1, because there is one player to leave after each game. and the champion remains