LeetCode: Ugly Number Solution
Recursively dividingApproach
For negative number, always
false
Keep dividing
2, 3, 5
, the number is ugly if the result is 1
Implementation
1var isUgly = function (n) {2 if (n <= 0) return false3 if (n % 2 === 0) return isUgly(n / 2)4 if (n % 3 === 0) return isUgly(n / 3)5 if (n % 5 === 0) return isUgly(n / 5)67 return n === 18}
References
Comments
Loading comments...
Tags
leetcode
recursion
math
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.