Advertisement
outlaws

primepi.php

Feb 18th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. /*
  3.   Generates Pi dots at increasing radius of i.
  4.   Ex. gnuplot to render:
  5.   set terminal jpeg size 1000,1000
  6.   set output 'primepi.jpeg'
  7.   unset xtics
  8.   unset ytics
  9.   unset ztics
  10.   unset key
  11.   unset tics
  12.   unset colorbox
  13.   set xrange[-201:201]
  14.   set yrange[-201:201]
  15.   plot 'primepi.dat' using 1:2 pt 20 ps 0.46 lc -1
  16. */
  17.  
  18. $primes = [];
  19. $radius = 200;
  20. $fout = fopen( "primepi.dat", "w" );
  21. get_prime( $primes, $radius + 1 );
  22.  
  23. for( $i = 0; $i <= $radius; $i++ ) {
  24.   $prime = $primes[$i];
  25.  
  26.   for( $j = 1; $j <= $prime; $j++ ) {
  27.     $y = $i * cos( 2 * M_PI * ( $j/$prime ) );
  28.     $x = $i * sin( 2 * M_PI * ( $j/$prime ) );
  29.     fprintf( $fout, "%5f\t%5f\n", $x, $y );
  30.   }
  31. }
  32.  
  33. function get_prime( Array &$primes, $n )
  34. {
  35.   ( $n >= 0 ) or die( "Out of range" );
  36.  
  37.   // Pushes a prime.
  38.   if( ! isset($primes[$n]) ) {
  39.     $count_primes = count($primes);
  40.  
  41.     while( $count_primes <= $n ) {    
  42.       if( $count_primes > 2 ) {
  43.         $c = $primes[$count_primes-1];
  44.         $found_prime = false;
  45.         while( ! $found_prime ) {
  46.           $c += 2;
  47.           $j;
  48.    
  49.           for( $j = 1; $j < $count_primes; $j++ ) {
  50.             if( $c % $primes[$j] == 0 ) {
  51.               break;
  52.             }
  53.           }
  54.           if( $j == $count_primes ) {
  55.             $found_prime = true;
  56.           }
  57.         }
  58.         $primes[] = $c;
  59.       }
  60.       else {
  61.         $primes = [1,2,3];
  62.       }
  63.       $count_primes = count($primes);
  64.     }
  65.   }
  66.   return $primes[$n];
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement