# Physics golf: inclined shooting

An angry bird is shot at an angle $$\β\$$ to the horizontal at a speed $$\u\$$. The ground is steep, inclined at an angle $$\α\$$. Find the horizontal distance $$\q\$$ that the bird traveled before it hit the ground.

Make a function $$\f(α, β, u)\$$ that returns the length $$\q\$$: the horizontal distance that the bird traveled before it hit the ground.

Constraints and notes:

• $$\-90° < α < 90°\$$.
• $$\0° < β < 180°\$$.
• $$\α < β\$$.
• $$\0 \le u < 10^9\$$.
• Assume acceleration due to gravity $$\g = 10\$$.
• You may use radians instead of degrees for $$\α\$$, $$\β\$$.
• Dimensions of $$\u\$$ are irrelevant as long as they are consistent with $$\g\$$ and $$\q\$$.
• No air resistance or anything too fancy.

Shortest code wins.

See the Wikipedia article on projectile motion for some equations.

Samples:

f(0, 45, 10) = 10
f(0, 90, 100) = 0
f(26.565, 45, 10) = 5
f(26.565, 135, 10) = 15

• As I saw some confusion about the formula, here it is for others to use it: q = ABS[1/5 u^2 Cos[β] Sec[α] Sin[β - α]] Feb 14, 2011 at 12:09

# Java

double q(double a, double b, double u){
return (Math.abs(((-Math.tan(a)+(Math.tan(b)))*(u*u)*(0.2*(Math.cos(b)*Math.cos(b))))));
}


Golfed Version (Thanks to Peter)

double z=u*Math.cos(b);return(Math.tan(b)-Math.tan(a))*z*z/5;


Maths Used:

$$q = ut\cos \beta \\ q\tan \alpha = ut\sin \beta - 0.5 \times 10 t^2 \\ - \tan \alpha + \tan \beta = 5\frac q {u^2} \sec^2 \beta \\ q = \frac {(\tan \beta - \tan \alpha)u^2} {5\sec^2 \beta }$$

• There is something wrong with this... I just cant figure out correctly, can some1 help? Feb 12, 2011 at 23:44
• This formula is not correct. Please see comment at gnibbler's post Feb 13, 2011 at 21:37
• So yet, we dont have any perfect solution :) Feb 13, 2011 at 23:06
• updated the formula... fire some testcases now please Feb 13, 2011 at 23:42
• You can save a few chars - Math.abs is unnecessary, -x+y is shorter as y-x, *0.2 is shorter as /5, and you have unnecessary brackets. OTOH you're missing the return type of the method. Feb 14, 2011 at 0:06

Based on Aman's solution:

q a b u=(tan a+tan b)*u*u*cos b^2/5


I think, this problem isn't real code-golf, as it is more implementing a formula than really doing some algorithm.

• Maybe you are right, since the formula is already too short. Feb 12, 2011 at 23:45
• Would something like /5 or /5. work?
– Nabb
Feb 13, 2011 at 15:51
• This formula is not correct. Please see comment at gnibbler's post. Feb 13, 2011 at 21:37

# Python3 - 65 chars

from math import*
f=lambda α,β,u:(tan(α)+tan(β))*u*u*.2*cos(β)**2

• That's not quite correct. 1) f should always be positive and 2) for α > 0 it returns a larger value than for a=0, which is not possible. Feb 13, 2011 at 21:36
• Ah well, I copied FUZxxl's formula :/ Feb 14, 2011 at 1:04