Wednesday, August 6, 2008

2 Maths problem

I. Find the acute angle between the minute and hour needle of a clock for a given time H:M?

Minute needle angle, m = M * (360/60) = 6M
Hour needle Angle, h = H * (360/12) + M * ( (360/12) / 60) = 30H + M/2

Solution 1:
Acute Angle = arccos(cos(h-m))

Hint: Cos() will bring all angle from -1 to 1 range and arccos() will bring that to 0 to 180(pi)

Solution 2:
Difference in angle, d = |h-m|
or
d = (h-m)%360
Opposite angle, od = 360 - d
Acute angle = 180 - x
Obtuse angle = 180 + x
where x = |(od - d)/2| = |180 - d|
i.e, Acute Angle = 180 - |180-d|

II. Given a circular queue of size n (0..n-1). Other variables are front and rear of the queue. Find a formula to find number of elements in queue from n, front and rear?

Solution:
number of elements = (rear - front) % n

That is the magic of mod operator.

No comments:

Linux Command Line and Shell Scripting Bible

 Linux Command Line and Shell Scripting Bible, 4th Edition, by Richard Blum and Christine Bresnahan is a complete guide for software profess...