We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
One way to think about it: Only perfect squares have odd factors, hence if we square back val (sqrt of the original number), we should get the original number back. If we don't get the original number, it means that it wasn't a perfect square and the int(...) truncated something.
Solution: if val**2 == num
E.g. sqrt(2) = 1.414. Next int(1.414) = 1. Finally, 1^2 != 2 (conclusion: 'NO' - not a perfect square hence even factors)
E.g. sqrt(169) = 13.0. Next int(13.0) = 13. Finally 13^2 == 169 (conclusion 'YES': perfect square hence odd factors)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Smart Number 2
You are viewing a single comment's thread. Return to all comments →
One way to think about it: Only perfect squares have odd factors, hence if we square back val (sqrt of the original number), we should get the original number back. If we don't get the original number, it means that it wasn't a perfect square and the int(...) truncated something.
Solution: if val**2 == num
E.g. sqrt(2) = 1.414. Next int(1.414) = 1. Finally, 1^2 != 2 (conclusion: 'NO' - not a perfect square hence even factors) E.g. sqrt(169) = 13.0. Next int(13.0) = 13. Finally 13^2 == 169 (conclusion 'YES': perfect square hence odd factors)