Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. /*
  2. Write a program to check whether a given number is an ugly number.
  3.  
  4. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
  5.  
  6. Note that 1 is typically treated as an ugly number.
  7. */
  8.  
  9. class Solution {
  10. public:
  11. bool isUgly(int num) {
  12. if (num <= 0) return false;
  13. while (num > 1) {
  14. if (num % 2 == 0)
  15. num /= 2;
  16. else if (num % 3 == 0)
  17. num /= 3;
  18. else if (num % 5 == 0)
  19. num /= 5;
  20. else return false;
  21. }
  22. return true;
  23. }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement