Created
December 18, 2014 15:54
-
-
Save soltrinox/27f8ddcce8826c020efb to your computer and use it in GitHub Desktop.
Prime factorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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