Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

"And now for something, completely different."

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.

Shooting

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

Constrains and notes:

  • -90 < α < 90.
  • 0 < β < 180.
  • α is always smaller than β.
  • 0 <= 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
share|improve this question
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[β - α]] – belisarius Feb 14 '11 at 12:09

3 Answers

up vote 2 down vote accepted

JAVA SOLUTION Works for radians only

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=u Cos(B) t
q tan(A) = u sin (B) t - .5 * 10 * t^2

- tan (A)  + tan(B) = 5q/u^2 sec^2 (B)
q =  [ - tan(A) + tan (B) ] u^2
    ---------------------
    sec^2(B)*5
share|improve this answer
There is something wrong with this... I just cant figure out correctly, can some1 help? – Aman ZeeK Verma Feb 12 '11 at 23:44
This formula is not correct. Please see comment at gnibbler's post – Eelvex Feb 13 '11 at 21:37
So yet, we dont have any perfect solution :) – Aman ZeeK Verma Feb 13 '11 at 23:06
1  
updated the formula... fire some testcases now please – Aman ZeeK Verma Feb 13 '11 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. – Peter Taylor Feb 14 '11 at 0:06
show 7 more comments

Python3 - 65 chars

from math import*
f=lambda α,β,u:(tan(α)+tan(β))*u*u*.2*cos(β)**2
share|improve this answer
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. – Eelvex Feb 13 '11 at 21:36
Ah well, I copied FUZxxl's formula :/ – gnibbler Feb 14 '11 at 1:04

Haskell (37 35)

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.