Skip to content

Instantly share code, notes, and snippets.

@awaemmanuel
Forked from cangoal/UglyNumber.java
Created April 14, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awaemmanuel/c16a2ecd4de0358c86d26e41bd611c6d to your computer and use it in GitHub Desktop.
Save awaemmanuel/c16a2ecd4de0358c86d26e41bd611c6d to your computer and use it in GitHub Desktop.
LeetCode - Ugly Number
//
public boolean isUgly(int num) {
if (num == 0) return false;
while (num % 2 == 0) num /= 2;
while (num % 3 == 0) num /= 3;
while (num % 5 == 0) num /= 5;
return num == 1;
}
//
public boolean isUgly(int num) {
if(num <= 0) return false;
int[] factor = {2, 3 ,5};
int i = 0;
while(num != 1){
int remainder = num % factor[i];
if(remainder == 0) num = num / factor[i];
else if(i < factor.length-1) i++;
else if(i == factor.length-1) break;
}
return num == 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment