Advertisement
Guest User

Prime Numbers Generators

a guest
May 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.75 KB | None | 0 0
  1. <?php
  2.  
  3.    
  4.  
  5. function prime0($n) {
  6.     // Lets time ourself.
  7.     $time_start = microtime(true);
  8.  
  9.     echo "  
  10.     <style>.test-table {float:left;} .test-table td {border:solid 1px #000;}</style>
  11.     <div>Prime Numbers = $n<br><br></div>
  12.     <table class='test-table'>
  13.     <tr><td colspan='2'>Dan Huckson prime0</td></tr>
  14.     <tr><td>Count</td><td>Prime Number</td></tr>";
  15.    
  16.     $count = 0;
  17.     for ($x = 2; $count < $n; $x++) {
  18.         $i = 2;
  19.         $prime = true;
  20.         $sqrt = sqrt($x);
  21.         if ($x > 2)
  22.             while ($i <= $sqrt)
  23.                 if ($x%$i++ == 0) {
  24.                     $prime = false;
  25.                     break;
  26.                 }
  27.        
  28.         if ($prime) echo "<tr><td>".(++$count)."</td><td>".($x)."</td></tr>";  
  29.     }
  30.    
  31.     echo "
  32.     </table>
  33.     <div>".(microtime(true) - $time_start)." seconds.</div>";
  34. }
  35.  
  36. function prime1($n) {
  37.     $time_start = microtime(true);
  38.  
  39.     echo "  
  40.     <style>.test-table {float:left;} .test-table td {border:solid 1px #000;}</style>
  41.     <table class='test-table'>
  42.     <tr><td colspan='2'>Dan Huckson prime1</td></tr>
  43.     <tr><td>Count</td><td>Prime Number</td></tr>";
  44.    
  45.     $count = 0;
  46.     $primes = array();
  47.    
  48.     for ($x = 2; $count < $n; $x++) {
  49.         $prime = true;
  50.        
  51.         if ($x > 2) {
  52.             foreach ($primes as $num) {
  53.                 if ($x%$num == 0) {
  54.                     $prime = false;
  55.                     break;
  56.                 }
  57.             }
  58.         }  
  59.        
  60.         if ($prime) echo "<tr><td>".(++$count)."</td><td>".($primes[] = $x)."</td></tr>";
  61.     }
  62.    
  63.    
  64.     echo "
  65.     </table>
  66.     <div>".(microtime(true) - $time_start)." seconds.</div>";
  67. }
  68.  
  69. function prime2($n) {
  70.     // $n equals the number of prime numbers to proccess.
  71.    
  72.     // Lets time ourself.
  73.     $time_start = microtime(true);
  74.    
  75.     // Display output in a table and include some styling.
  76.     echo "  
  77.     <style>.test-table {float:left;} .test-table td {border:solid 1px #000;}</style>
  78.     <table class='test-table'>
  79.     <tr><td colspan='2'>Dan Huckson prime2</td></tr>
  80.     <tr><td>Count</td><td>Prime Number</td></tr>";
  81.    
  82.     $count=0; // Prime numbers processed, or index counter.
  83.    
  84.     // We will continue to loop while our current,
  85.     // $count (processed prime numbers) < $n (number of prime numbers to proccess) .
  86.     // $i holds the current number being tested.
  87.     // We are starting at 2, which will be our first prime number and continue to we find $n prime numbers.
  88.     for ($i=2; $count < $n ; $i++) {
  89.         // Set the result to true and change it to false if the current number ($i) is not a prime number.
  90.         $result = TRUE;
  91.        
  92.         // Loop through all numbers from 2 to ($i/2 + 1) or (half of $i + 1).
  93.         // If $i is not a prime number the loop is termanated by breaking out of the loop.
  94.         $x = $i/2+1;
  95.         for ($j=2; $j<$x; $j++) {
  96.             if (!($i%$j)) { // If we have no remainder the number is not a prime number.
  97.                 $result=FALSE; // Set result to false not a prime number.
  98.                 break; // Save some CPU cycles, no need to continue looping if number is not prime a number.
  99.             }
  100.         }      
  101.         if ($result) // If $result is true ($i) is a prime number, update count and display results.
  102.             echo "<tr><td>".(++$count)."</td><td>$i</td></tr>";
  103.     }
  104.  
  105.     // All done, Calculate and display total run time.
  106.     echo "
  107.     </table>
  108.     <div>".(microtime(true) - $time_start)." seconds.</div>";
  109. }
  110.  
  111. function prime3($n) {
  112.     // $n equals the number of prime numbers to proccess.
  113.    
  114.     // Lets time ourself.
  115.     $time_start = microtime(true);
  116.    
  117.     echo "  
  118.     <style>.test-table {float:left;} .test-table td {border:solid 1px #000;}</style>
  119.    
  120.     <table class='test-table'>
  121.     <tr><td colspan='2'>Dan Huckson prime3</td></tr>
  122.     <tr><td>Count</td><td>Prime Number</td></tr>";
  123.    
  124.     $count=0;
  125.     for ($i=2; $count < $n; $i++) {
  126.         if (!gmp_intval(gmp_mod(gmp_add(gmp_fact(($i-1)),1),$i)))
  127.             echo "<tr><td>".(++$count)."</td><td>$i</td></tr>";
  128.     }
  129.  
  130.     echo "
  131.     </table>
  132.     <div>".(microtime(true) - $time_start)." seconds.</div>";
  133. }
  134.  
  135.    
  136. function prime4($n) {
  137.     $time_start = microtime(true);
  138.    
  139.     $total = 0;
  140.     echo "  
  141.     <style>.test-table {float:left;} .test-table td {border:solid 1px #000;}</style>
  142.     <table class='test-table'>
  143.     <tr><td colspan='2'>Geoff</td></tr>
  144.     <tr><td>Count</td><td>Prime Number</td></tr>";
  145.    
  146.     for ($x = 1; $total < $n; $x++) {
  147.         $count = 2;
  148.         for ($y=1; $y <= $x; $y++) {
  149.             if (!($x % $y) && !($count--)) break;
  150.         }
  151.         if(!$count) {
  152.             echo "<tr><td>".(++$total)."</td><td>$x</td></tr>";
  153.         }
  154.     }
  155.     echo "
  156.     </table>
  157.     <div>".(microtime(true) - $time_start)." seconds.</div>";
  158. }
  159.  
  160. set_time_limit(0);
  161. echo '
  162. <!DOCTYPE html>
  163. <html>
  164.     <head>
  165.         <meta charset="UTF-8">
  166.         <title></title>
  167.     </head>
  168.     <body>';
  169.         prime0(1000);
  170.         prime1(1000);
  171.         prime2(1000);
  172.         prime3(1000);
  173.         prime4(1000);
  174.     echo '
  175.     </body>
  176. </html>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement