Sunday, July 28, 2019

Find Angle MBC - Hacker Rank Solution

  is a right triangle, at.
Therefore,.
Point is the midpoint of the hypotenuse .
You are given the lengths and.
Your task is to find  (angle, as shown in the figure) in degrees.
Input Format
The first line contains the length of the side .
The second line contains the length of the side .
Constraints

  • Lengths and are natural numbers.
Output Format
Output in degrees. 
Note: Round the angle to the nearest integer.
Examples:
If the angle is 56.5000001°, then output 57°.
If the angle is 56.5000000°, then output 57°.
If the angle is 56.4999999°, then output 56°.
Sample Input
10
10
Sample Output
45°

Find Angle MBC - Hacker Rank Solution

On careful observation, one can prove that . Hence, the solution is .
Most languages have a builtin  function, commonly named atan. Here's an example implementation in Python, using atan2:
from math import *
print "%.0f°" % degrees(atan2(float(raw_input()), float(raw_input())))
There are many possible proofs that  = . This is my favorite one.
Since  is a right angle, let  be the fourth point such that  is a rectangle.
It looks like the following:
image
Now, draw both diagonals  and . Since the diagonals of any rectangle bisect each other, it follows that the point of intersection of  and  must each be their midpoints. But we defined  to be the midpoint of , hence  must be the midpoint of  as well, hence  bisects .
Now, we have:
  •  (opposite sides of a rectangle have the same length)
  •  (both are right angles since  is a rectangle)
  •  (they're the same side).
Hence,  and  are congruent. This implies that , which is what we wanted to prove. (Note that angle  is the same as angle .)

Python 2

import cmath
import math

AB = int(raw_input())
BC = int(raw_input())

print str(int(round(math.degrees(cmath.phase(complex(BC,AB))))))+'°'

2 comments:

  1. it has non-ascii values. It does not accept those

    ReplyDelete
  2. dont accept submissions with non ASCII characters for this challenge

    ReplyDelete

Powered by Blogger.