Guest User

Untitled

a guest
Mar 23rd, 2018
491
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <div id="result"></div>
  8. <div id="time"></div>
  9.  
  10. <script type="text/javascript">
  11. let performanceBeginning = performance.now();
  12.  
  13.  
  14. let startPoint   = 10000,
  15.     endPoint     = 100000,
  16.     primeNumbers = [],
  17.     palindromes  = [];
  18.  
  19. var palarray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  20.  
  21. primeNumbers = calcPrimeNumbers(startPoint, endPoint);
  22. let perfPrimes = performance.now();
  23. palindromes  = multiplyNumbers(primeNumbers);
  24.  
  25. showResult(palindromes);
  26.  
  27. // find all prime numbers in provided segment
  28. function calcPrimeNumbers(startPoint, endPoint) {
  29.   let array = [];
  30.  
  31.   // we wanna do += 2
  32.   array.push(2);
  33.   // test i for being prime
  34.   for (let i = 3; i < endPoint; i+=2) {
  35.       // loop through found primes
  36.       let isPrime = true;
  37.       for (let j = 0; (j < array.length) && (array[j]*array[j] < i); j++) {
  38.           // check if divisor
  39.           if ( i % array[j] == 0) {
  40.               isPrime = false;
  41.               break;
  42.           }
  43.       }
  44.       if (isPrime)
  45.           array.push(i);
  46.   }
  47.  
  48.   return array;
  49. }
  50.  
  51. // multiply all numbers in array with themselves and return the biggest number
  52. function multiplyNumbers(array) {
  53.  
  54.   // loop through all values in array
  55.     // "i" variable is an index of the current multiplicand in array
  56.     let ist = 0;
  57.     let jst = 0;
  58.     let biggestPalindrome = 0;
  59.     for (let i = 0; i < array.length; i++) {
  60.         // loop through the rest of array values
  61.         for (let j = i + 1; j < array.length; j++) {
  62.  
  63.             let product = array[i] * array[j];
  64.  
  65.             // check if product is palindrome and bigger than previous biggest number
  66.             if (product > biggestPalindrome && isPalindrome(product)) {
  67.                 ist = i;
  68.                 jst = j;
  69.                 biggestPalindrome = product;
  70.             }
  71.         }
  72.     }
  73.  
  74.     return {
  75.         multiplier: array[ist],
  76.         multiplicand: array[jst],
  77.         product: biggestPalindrome
  78.     };
  79. }
  80.  
  81.  
  82. function isPalindrome(number) {
  83.     let i = 0;
  84.     for (i = 0; number > 0; i++)
  85.     {
  86.         palarray[i] = number % 10;
  87.         number = ~~(number/10);
  88.     }
  89.     for (let j = i >> 1; j >=0; j--)
  90.     {
  91.         if (palarray[j] != palarray[i - j - 1])
  92.             return false;
  93.     }
  94.     return true;
  95. }
  96.  
  97. function showResult(result) {
  98.   let string = JSON.stringify(result);
  99.   let div = document.getElementById('result');
  100.  
  101.   div.innerHTML = string;
  102. }
  103.  
  104. let performanceEnd = performance.now();
  105. document.getElementById('time').innerHTML = 'Script has been executed in ' + ((performanceEnd - performanceBeginning) / 1000)
  106.     + ' seconds primes in ' + ((perfPrimes - performanceBeginning) / 1000);
  107.  
  108. </script>
  109.  
  110.  
  111. </body>
  112. </html>
RAW Paste Data