Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.lang.StringBuilder;
  3.  
  4.  
  5. public class MyClass {
  6.  
  7. //Check Prime
  8.  
  9. public static boolean isPrime(int n){
  10.  
  11. for(int i=2;i<n/2;i++){
  12. if(n%i==0){
  13. return false;}
  14. }
  15.  
  16. return true;}
  17.  
  18.  
  19.  
  20. public static void main(String args[]) {
  21.  
  22. long total=1;
  23.  
  24. //Number to be tested
  25.  
  26. int n=6;
  27.  
  28. ArrayList<String> pFactors = new ArrayList<String>();
  29.  
  30. //Convert to Factorial
  31.  
  32. for( int i=0;i<(n);i++){
  33. total += total*i;}
  34.  
  35. //Number to decompose
  36.  
  37. long temp = total;
  38.  
  39.  
  40. for(int i=2;i<=temp;i++){
  41.  
  42. int count =0;
  43.  
  44. if(isPrime(i)){
  45.  
  46. while(temp%i==0){
  47. count++;
  48. temp = temp/i;}
  49.  
  50. //Output didnt want an exponent where the exponent is one
  51. if(count==1){
  52. pFactors.add(Integer.toString(i));}
  53. //Ignores any values where the exponent is zero
  54. else if(count>0){
  55. pFactors.add(Integer.toString(i) + "^" + Integer.toString(count));} // EG: 2^5. i=2 and count =5
  56.  
  57. }
  58. }
  59.  
  60.  
  61. for(int i=0;i<pFactors.size();i++){
  62.  
  63. System.out.println(pFactors.get(i));
  64.  
  65. }
  66.  
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement