TheAlgorithms / Ruby

All algorithms implemented in Ruby
MIT License
1.15k stars 289 forks source link

Angle Between Hands of a Clock Algorithm #174

Closed vbrazo closed 2 years ago

vbrazo commented 3 years ago

Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.

How to calculate the two angles with respect to 12:00? The minute hand moves 360 degrees in 60 minutes (or 6 degrees in one minute) and the hour hand moves 360 degrees in 12 hours(or 0.5 degrees in 1 minute). In h hours and m minutes, the minute hand would move (h * 60 + m) * 6, and the hour hand would move (h * 60 + m) * 0.5.

Constraints:

1 <= hour <= 12 0 <= minutes <= 59 Answers within 10^-5 of the actual value will be accepted as correct.