Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created December 18, 2014 15:54
Show Gist options
  • Save soltrinox/27f8ddcce8826c020efb to your computer and use it in GitHub Desktop.
Save soltrinox/27f8ddcce8826c020efb to your computer and use it in GitHub Desktop.
Prime factorization
public static String primeFactorization(long num){ //method signature. takes long, returns string of factorization
String ans=""; //creates the answer
for(int i=2;i<=num;i++){ //loops from 2 to the num
if(num%i==0){ //checks if i is a divisor of num
ans+=i+"*"; //writes i in prime factorization
num=num/i; //since it is written down, num=num/i
i--; //just in case their are multiple factors of same number. For example, 12=2*2*3
}
}
return(ans.substring(0,ans.length()-1)); //takes away last asterisk. Factorization of 4=2*2*=>2*2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment