Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.lang.StringBuilder;
  3.  
  4. class FactDecomp {
  5.  
  6. public static boolean isPrime(long n){
  7. for(int i=2;i<n/2;i++){
  8. if(n%i==0){
  9. return false;}}
  10. return true;
  11. }
  12.  
  13.  
  14. public static String decomp(int n) {
  15. long total=1;
  16.  
  17. ArrayList<String> pFactors = new ArrayList<String>();
  18.  
  19. //Convert to Factorial
  20.  
  21. for( int i=0;i<(n);i++){
  22. total += total*i;}
  23.  
  24. for(long i=2;i<=total;i++){
  25. int count =0;
  26. if(isPrime(i)){
  27. while(total%i==0){
  28. count++;
  29. total = total/i;
  30. }
  31. // System.out.println(temp);
  32. if(count==1){
  33. pFactors.add(Long.toString(i));}
  34. else if(count>0){
  35. pFactors.add(Long.toString(i) + "^" + Integer.toString(count));}
  36.  
  37.  
  38. }
  39. }
  40. StringBuilder output = new StringBuilder();
  41. for(int i=0;i<pFactors.size();i++){
  42. output.append(pFactors.get(i));
  43. if(i!=pFactors.size()-1){
  44. output.append(" * ");}}
  45. return output.toString();
  46.  
  47.  
  48.  
  49.  
  50.  
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement