Last active
April 3, 2020 18:19
-
-
Save krowter/9817b42d96d2f6dee407187b57519fd0 to your computer and use it in GitHub Desktop.
Finding instances where an analog clock's minutehand overlaps the hourhand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Note: kode ini hanya untuk ilustrasi metode brute-force | |
function getAngle({ hour, minute, second, miliSecond }) { | |
//Semua perhitungan di bawah dikalikan 10000 untuk menyimpan akurasi | |
//dari operasi bilangan. Nanti akan dibagi 10000 lagi. | |
//Cari proporsi sudut menit, detik, dan milidetik dalam satu tick jarum menit | |
const minuteHand = | |
minute * 60000 + (second * 10000) / 10 + (miliSecond * 10000) / 10000; | |
//Cari kontribusi sudut dari jam, menit, detik, dan milidetik dalam satu jam | |
const hourHand = | |
hour * 300000 + | |
(minute * 10000) / 2 + | |
(second * 10000) / 120 + | |
(miliSecond * 10000) / 120000; | |
//ambil nilai absolut karena kadang jarum jam | |
//memiliki sudut yang lebih besar | |
let angle = Math.abs(minuteHand - hourHand); | |
//ambil sudut interior saja | |
angle /= 10000; | |
if (angle > 180) { | |
angle = 360 - angle; | |
} | |
return angle; | |
} | |
//sinulasikan pergerakan jarum jam, menit, dan detik | |
//seperti pada jam analog | |
for (let hour = 0; hour < 12; ++hour) { | |
for (let minute = 0; minute < 60; ++minute) { | |
for (let second = 0; second < 60; ++second) { | |
for (let miliSecond = 0; miliSecond < 1000; miliSecond += 1) { | |
const angle = getAngle({ hour, minute, second, miliSecond }); | |
//kalau sudut di antara jarum jam dan menit mendekati atau sama dengan 0 | |
//cetak timestamp-nya | |
if (angle < 0.0001) { | |
console.log( | |
`${angle} derajat pada jam ${hour}:${minute}:${second}:${miliSecond}` | |
); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment