SHARE
TWEET

Untitled

a guest Sep 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function reset(n) {
  2.     workingNum = n; run = false; end = false; check = false; str = '';
  3. }
  4.  
  5. function initialTest(n) {
  6.     for (i = 2; i < n; i++) {
  7.         if (n % i === 0) {check = true; return;}
  8.         if (n / i < i) return;
  9.     }
  10. }
  11.  
  12. function isPrime(n) {
  13.     for (i = 2; i < n; i++) {
  14.         if (n % i === 0) {run = false; return;}
  15.     }
  16.     currentPrimeNum = i; run = true;
  17. }
  18.  
  19. function concat(sum, n) {
  20.     if (sum === 1) str = str + '(' + n + ')';
  21.     else str = str + '(' + n + '**' + sum + ')';
  22. }
  23.  
  24. function calculate(n) {
  25.     var sum = 0; var divide = true;
  26.     while (divide === true) {
  27.         if (workingNum % n === 0) {workingNum /= n; sum++; divide = true;}
  28.         else divide = false;
  29.     }
  30.     if (sum >= 1) concat(sum, n);
  31.     if (workingNum === 1) {end = true; return;}
  32.     var final = true;
  33.     var x = 2;
  34.     while (final) {
  35.         if (workingNum % x === 0) final = false;
  36.         if (workingNum / x < x) {str = str + '(' + workingNum + ')'; end = true; return;}
  37.         x++;
  38.     }
  39.     if (final === true) {str = str + '(' + workingNum + ')'; end = true;}
  40. }
  41.  
  42. function main(n) {
  43.     reset(n);
  44.     initialTest(n);
  45.     if (!check) return;
  46.     for (num = 2; num < n; num++) {
  47.         isPrime(num);
  48.         if (run) calculate(currentPrimeNum);
  49.         if (end) return;
  50.     }
  51. }
  52.  
  53. function primeFactors(n) {
  54.     main(n);
  55.     if (!check) {var prime = '(' + n + ')'; return prime;}
  56.     return str;
  57. }
RAW Paste Data
Top