How would I find the distance between the point (2,-3,1) and the plane 3x+y-2z=15
The distance from P(x,y,z) to the plane ax+by+cz=d is:
|ax+by+cz-d| / sqrt(a^2 + b^2 + c^2)
Therefore |ax+by+cz-d| = |3(2) + (-3) + (-2) - 15| = |-14| = 14.
sqrt(a^2 + b^2 + c^2) = sqrt (9 + 1 + 4) = sqrt14.
The distance is 14/sqrt14 or sqrt 14.
Showing posts with label math. Show all posts
Showing posts with label math. Show all posts
Jan 15, 2009
Dec 29, 2008
Use 1-5 random generator to generate 1-7 random number
Use 1-5 random generator to generate 1-7 random number
Hint: How to do it with bit level
Roll the dice twice: and simply take XOR of result1 and ~result2, reject 7, keep 0~6.
explanation:
result1 is: 001 010 011 100 101
~result2 is: 110 101 100 011 010
You can see for each bit position the prob. of 1 bits is equal to prob. of 0 bits, and xor is also uniform distribution, thus result1 xor ~result2 is uniform distribution
Hint: How to do it with bit level
Roll the dice twice: and simply take XOR of result1 and ~result2, reject 7, keep 0~6.
explanation:
result1 is: 001 010 011 100 101
~result2 is: 110 101 100 011 010
You can see for each bit position the prob. of 1 bits is equal to prob. of 0 bits, and xor is also uniform distribution, thus result1 xor ~result2 is uniform distribution
Use 1-6 random generator to generate 1-7 random number
Use 1-6 random generator to generate 1-7 random number
int foo()
{
do
{
int x=rand6-1;//0~5
int y=rand6-1;
}
while((x*6+y)==35); //0~34 is valid
return (x*6+y)%7+1;//1~7
}
There are 80 pieces of gold. One of them is lighter. You can weigh only 4 times to find out which one is lighter.
There are 80 pieces of gold. One of them is lighter. You can weigh only 4 times to find out which one is lighter.
Think from bottom to up, one time measure, we can find out 1 lighter out of 3, one more time measure, we can find lighter coin within 3 coins out of 9 coins...
1 1 1
3 3 3
9 9 9
27 27 27
So 4 times is enough to find the one lighter coin out of 81 coins, log_3(N)
Think from bottom to up, one time measure, we can find out 1 lighter out of 3, one more time measure, we can find lighter coin within 3 coins out of 9 coins...
1 1 1
3 3 3
9 9 9
27 27 27
So 4 times is enough to find the one lighter coin out of 81 coins, log_3(N)
Dec 28, 2008
How to decide the number can be divisible by 3
How to decide the number can be divisible by 3
(no. of even bit – no. of odd bit) is divisible by 3, the number is divisible by 3
(no. of even bit – no. of odd bit) is divisible by 3, the number is divisible by 3
Dec 24, 2008
You have a 100 story building and two eggs. These are especially strong eggs. There is some floor below which the egg will not break if dropped.
You have a 100 story building and two eggs. These are especially strong eggs. There is some floor below which the egg will not break if dropped. What is the worst case upper bound on the number of drops you must make to determine this floor?
Let N the number of drops you need to find the first floor that breaks eggs. Go to the Nth floor and drop an egg. If it breaks you have N - 1 more drops to test the N - 1 floors below. If it doesn't breaks, go to the N + (N - 1)th floor and drop the same egg. If it breaks you have N -2 drops left to test the N - 2 floors between Nth floor and 2N -1 floor. By a similar analysis you approach the top of the building with
N + (N - 1) + (N - 2) + (N - 3) + . . . + 1 <= 100
N ( N + 1 ) / 2 <= 100
This is a quadratic equation which yields N <= 13.65. If N = 13 you can only analyze a building of 91 floors. It takes N = 14 to test a 100 floor building. Final solution. You go to the 14th floor and drop an egg. If it breaks you have 13 more drops to test floors 1 to 13. If it doesn't break you go to the 27th (14 + 13) and drop the first egg again. If it breaks you have 12 drops left to test the 12 floors above 14 and below 27. You continue up the building until you reach the 99th floor (14+13+12+11+10+9+8+7+6+5+4) with three drops left. If it breaks you have three drops left to test the three floors between the 95th floor and the 99th floor.
3 eggs: for first egg, we use binary search, for the rest two eggs we compute like N(N+1)<=50, so maximum time is 10+1(not optimized )
N floors, m eggs, l drops
C(l,1)+C(l,2)+…+C(l,m)<n
Let N the number of drops you need to find the first floor that breaks eggs. Go to the Nth floor and drop an egg. If it breaks you have N - 1 more drops to test the N - 1 floors below. If it doesn't breaks, go to the N + (N - 1)th floor and drop the same egg. If it breaks you have N -2 drops left to test the N - 2 floors between Nth floor and 2N -1 floor. By a similar analysis you approach the top of the building with
N + (N - 1) + (N - 2) + (N - 3) + . . . + 1 <= 100
N ( N + 1 ) / 2 <= 100
This is a quadratic equation which yields N <= 13.65. If N = 13 you can only analyze a building of 91 floors. It takes N = 14 to test a 100 floor building. Final solution. You go to the 14th floor and drop an egg. If it breaks you have 13 more drops to test floors 1 to 13. If it doesn't break you go to the 27th (14 + 13) and drop the first egg again. If it breaks you have 12 drops left to test the 12 floors above 14 and below 27. You continue up the building until you reach the 99th floor (14+13+12+11+10+9+8+7+6+5+4) with three drops left. If it breaks you have three drops left to test the three floors between the 95th floor and the 99th floor.
3 eggs: for first egg, we use binary search, for the rest two eggs we compute like N(N+1)<=50, so maximum time is 10+1(not optimized )
N floors, m eggs, l drops
C(l,1)+C(l,2)+…+C(l,m)<n
Dec 19, 2008
Write an algorithm to check unsigned integer is a multiple of 3, without using division and modulo operators.
Write an algorithm to check unsigned integer is a multiple of 3, without using division and modulo operators.
Get each decimal digit from the integer number, sum up them, if they can be divisible by 3. the number is multiple of 3
Like 123456,
1+2+3+4+5+6=21
1+2=3 so 123456 can be divisible by 3.
Get each decimal digit from the integer number, sum up them, if they can be divisible by 3. the number is multiple of 3
Like 123456,
1+2+3+4+5+6=21
1+2=3 so 123456 can be divisible by 3.
Bool Foo(int num)
{
While(true)
{
num=Sum up all decimal digits of num;
If(num==3||num==6||num==9)
Return true;
If(num<3)
Return false;
}
Subscribe to:
Posts (Atom)