Bad Dinosaur!

You Should Check This Out

CollegeJobConnect | Better Undergraduate Recruiting

Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Friday, November 13, 2009

Male / Female + Solution

Math question:

"In a country in which people only want boys, every family continues to have children until they have a boy. If they have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to girls in the country?"

Please post your solution + answer in the comments. GO!


----- Solution -----

Thank you to all that emailed me and posted in the comments! The first responder with the correct answer was Gary H., however, he volunteered to disqualify himself because he had seen the question before (what a noble man!).

The winner is Paul R. A case of Snuggies is on it's way to you as your prize.

Honorable mention goes out to Dan D and Adam C. No, no honorable mention goes to Bad Dinosaur, he didn't even play.

Here it is: we assume that the probabilities of having a boy vs girl is 50%. Next, assume there are N families, so there will be N boys at the end (because the families keep going until they have a boy. N families. N boys). From the girl side, we can see the following:

round 1: N/2 have a boy and stop, N/2 have a girl, so N/2 girls so far
round 2: N/2 families left, 50% have a girl, so N/4 (0.5*N/2 = N/4)
round 3: N/4 families left, 50% have a girl, so N/8 (0.5*N/4 = N/8)

You get the idea. There will be an "infinite" sum equal to:

N/2 + N/4 + N/8 ... N / infinity = N


Note: this follows from Series basics (read more here).

So number of boys equals N, and number of girls equals N, the ratio is N:N or 1:1

The Monte Carlo simulation supports these findings (thanks to Paul R. for the lovely ruby code):
Example output:

Family problem, 1000000 trials:

997496 girls, 1000000 boys
1.997496 children per family
0.997496 girls per boy


#!/usr/bin/env ruby
# usage: ./family.rb [trials]

# Family problem, 1000000 trials:
#
# 997496 girls, 1000000 boys
# 1.997496 children per family
# 0.997496 girls per boy
#
# Ran in 15.508096s

class Array
def rand # A spoonful of sugar helps the medicine go down
self[(length*Kernel.rand).floor]
end
end

class Family
def initialize(complete = true)
@children = { :boy => 0, :girl => 0 }
complete! if complete
end

def boys
@children[:boy]
end

def girls
@children[:girl]
end

def have_child!
@children[[:boy, :girl].rand] += 1
end

def complete!
have_child! until complete?
end

def complete?
boys == 1
end
end

iterations = if ARGV.length > 0
ARGV[0].to_i
else
1000
end

puts "Family problem, #{iterations} trials:"

started = Time.new

children = { :boys => 0, :girls => 0 }
iterations.times do
f = Family.new
children[:boys] += f.boys
children[:girls] += f.girls
end

family_size = (children[:boys] + children[:girls]).to_f / iterations
ratio = children[:girls].to_f / children[:boys]

elapsed = Time.new - started

puts
puts "#{children[:girls]} girls, #{children[:boys]} boys"
puts "#{family_size} children per family"
puts "#{ratio} girls per boy"
puts
puts "Ran in #{elapsed}s"

Wednesday, August 5, 2009

Triangle Problem + Solution

Question: "If you break a straight line randomly in two places, what is the probability that you can form a triangle from the resulting three pieces?"

Please provide the proof + code for a simulation of 10,000 trails! Will post the solution tomorrow so get crackin!

------------------------------------------

Solution:

First off, congratulation are in order to a Mr. Paul R. and a Mr. Dan D. who both successfully utilized the higher power of numbers and reached the correct answer. A slow clap to you both. A special mention is in order for a Mr. Frank I. who uncovered about 80% of the solution, and to a Mr. Elliot G. who at least attempted the problem (percentage of success was not capable of being calculated for this last attempt).

The Proof. We first have to determine what, when given three different lines of varying lengths, is required to construct a Triangle? In order to create a triangle from three different lines of varying lengths, we need the following condition to hold:

Long side < short side + medium side


Now, looking at the posed question, lets say that the stick’s length is L, and we have two random breaks, a and b. We can draw the following representation:


We can see that, in this case, a < b. We can also see that there are three lengths that result from this break, x, y, and z. We have the following equations now:

1. x + y + z = L
2. a < b
3. x = a
4. y = b – a
5. z = L – b

So we have to think about the value-space that a and b can take on, namely that 0 < a < L and 0 < b < L, or graphically:


This is a representation of all possible values a and b can take on. Now let’s assume that x >= y >= z so from the starting condition that must be fulfilled to make a triangle, we have:

x < y + z => a < (b – a) + (L – b) => a < -a + L = a < L/2

So in order for a triangle to be the result, a must be less than half of the entire length. Furthermore, because x = a and x is the largest, y and z must also be less than half of length L. We see that no single side can be greater than or equal to L/2 if we want to be able to make a triangle from the sides.

So we can update our equations:

1. x + y + z = L
2. a < b
3. x = a and x < L/2
4. y = b – a and y < L/2
5. z = L – b and z < L/2

So we can now update our set of values drawing – we know that a < b and a < L/2 from updated equation #3, so:


The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Next, from updated equation #5 we have that L – b < L / 2, which equals b > L / 2:


The values of that a and b can take on, when a < b, that will produce a triangle are now outlined in green. Finally from updated equation #4 we have that b – a < L / 2, which equals b < a + L / 2 (this is a linear condition – line has a slope of 1 and b intercept of L/2:


So the area that meets all of these conditions is our winner (in green):


Which we can see is equal to 1/8 (0.125) of the possible values.

However, remember that this assumes that a < b, and because we know that a and b are both random numbers, they are interchangeable, so we can also have the following situation:


So this scenario must be taken into account. We now assume a > b, which yields the following starting equations:

1. x + y + z = L
2. a > b
3. x = b and x < L/2
4. y = a – b and y < L/2
5. z = L – a and z < L/2

We are still working with the same initial work space for a and b, but we have a different limitation being applied, namely a > b.

If we rearrange all these new equations as we did above, we arrive at the following conditions:

Starting Condition: a > b
From #3: b < L / 2
From #4: b > a – L / 2
From #5: a > L / 2

Which we can plot graphically:


And the only area of intersection for all equations mirrors what we found before, reflected across the line b = a:


Finally, we combine both scenarios for a > b and b < a and the resulting areas that satisfy our starting stipulation (long < medium + short), and we get the following result set:


Which yields and answer of 1/8 + 1/8 = 1/4 or 0.25 or 25%. So if you break a straight line randomly in two places, the probability that you can form a triangle from the resulting three pieces is 25%.

The Code. The code to run the simulation. This is written in Ruby.

#!/usr/bin/ruby
numOfTrials = 10000
numTriangles = 0

numOfTrials.times do
# generate two random breaks, ordered
breaks = [ rand, rand ].sort

# load the length calculations via the breaks into an "lengths" array
# order the lenghts
lengths = [

breaks[0] ,
breaks[1] - breaks[0] ,
1 - breaks[1]

].sort

# add 1 to triangle var if we have a triangle
numTriangles += 1 if lengths[2] < lengths[0] + lengths[1]

end

pct = numTriangles.to_f / numOfTrials * 100

puts "Number of Trials: " + numOfTrials.to_s
puts "Number of Triangles: " + numTriangles.to_s
puts "Percentage: %" + pct.to_s

Running this simulation produces results that cluster around the expected value of 25%.