Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. /*
  2.   Name: PrimeFinder
  3.   Author: Rik
  4.   Description: Finds the nth prime using a modification of the sieve of eratosthenes
  5. */
  6.  
  7.  
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <memory.h>
  12.  
  13.  
  14. #define _DEBUG_
  15.  
  16.  
  17.  
  18.  
  19. const unsigned int uiNumberOfPrimesToFind = 10001;
  20.  
  21.  
  22.  
  23. int main()
  24. {
  25.  
  26.  time_t timeStart, timeEnd;
  27.  
  28.  #ifdef _DEBUG_
  29.  unsigned int uiDivisions = 0;
  30.  unsigned int uiIncrements = 0;
  31.  #endif  
  32.    
  33.  
  34.  unsigned int uiPrimesFound = 0;
  35.  
  36.  unsigned int uiPrimeList[uiNumberOfPrimesToFind];
  37.  memset(&uiPrimeList,0,(uiNumberOfPrimesToFind*sizeof(unsigned int)));
  38.  
  39.  
  40.  
  41.  
  42.  time(&timeStart);  
  43.  
  44.  /***********************************************************************************/
  45.  /*   2 is the only even prime number. Code afterwards only checks for odd primes.  */                                          
  46.  /***********************************************************************************/
  47.  
  48.  uiPrimeList[uiPrimesFound] = 2;
  49.  ++uiPrimesFound;
  50.  
  51.  
  52.  /*********************************************************/
  53.  /*   uiIncrement and uiMinimum should be even integers   */
  54.  /*********************************************************/
  55.  
  56.  unsigned int uiRounds = 0;
  57.  unsigned int uiIncrement = 367398; // Optimum number for finding the 31337th prime
  58.  
  59.  bool bFirstIncrement = true;
  60.  
  61.  
  62.  while( uiPrimesFound < uiNumberOfPrimesToFind )
  63.  {
  64.  
  65.      unsigned int uiMinimum = 2 + (uiRounds*uiIncrement);
  66.      unsigned int uiMaximum = uiMinimum + uiIncrement;
  67.      
  68.      unsigned int uiListSize = ((uiMaximum - uiMinimum)>>1);
  69.      
  70.      #ifdef _DEBUG_
  71.      //++uiDivisions;
  72.      ++uiIncrements;
  73.      #endif
  74.      
  75.      unsigned int uiTempNumberList[uiListSize];
  76.      
  77.      unsigned int uiNewOddNum = (uiMinimum+1);
  78.      
  79.      for( unsigned int uiIndex = 0; uiIndex < uiListSize; ++uiIndex,uiNewOddNum+=2 )  uiTempNumberList[uiIndex] =  uiNewOddNum;    
  80.      
  81.      
  82.      
  83.      
  84.    
  85.      for( unsigned int uiIndex = 0; uiIndex < uiListSize; ++uiIndex )
  86.      {
  87.         unsigned int uiPrimeCandidate = uiTempNumberList[uiIndex];
  88.            
  89.         if( uiPrimeCandidate ) {
  90.        
  91.          bool bIsPrime = true;
  92.          
  93.          
  94.          if( !bFirstIncrement )
  95.          {
  96.                  unsigned int uiPrime = 0;
  97.                  unsigned int uiFractionOfCandidate = 0;
  98.                  for( unsigned int uiPrimeListIndex = 0;  uiPrimeListIndex < uiPrimesFound; ++uiPrimeListIndex )
  99.                    {
  100.                        #ifdef _DEBUG_
  101.                        uiDivisions+=2;
  102.                        #endif
  103.                        
  104.                      
  105.                       uiPrime = uiPrimeList[uiPrimeListIndex];
  106.                      
  107.                       if( uiPrimeListIndex > 0 )
  108.                       {
  109.                          uiFractionOfCandidate = (uiPrimeCandidate/uiPrimeList[uiPrimeListIndex-1]);
  110.                         if( uiPrime > (uiFractionOfCandidate) ) break;
  111.                       }
  112.                      
  113.                       if( ( uiPrimeCandidate % uiPrime )  == 0 )
  114.                         {
  115.                              bIsPrime = false;
  116.                              break;
  117.                         }
  118.                    }
  119.         }
  120.            
  121.        
  122.            
  123.         if( bIsPrime )
  124.         {
  125.             uiPrimeList[uiPrimesFound] = uiPrimeCandidate;
  126.             ++uiPrimesFound;
  127.            
  128.             if( uiNumberOfPrimesToFind == uiPrimesFound ) break;
  129.            
  130.             unsigned int uiIndex2 = uiIndex;
  131.             while( uiIndex2 < uiListSize ) {  uiTempNumberList[ uiIndex2 ] = 0; uiIndex2 += uiPrimeCandidate;  }
  132.         }
  133.     }
  134.    
  135.      }
  136.      
  137.      
  138.      bFirstIncrement = false;
  139.      ++uiRounds;
  140.      
  141.  }
  142.  
  143.  
  144.  
  145.   time(&timeEnd);  
  146.    
  147.  printf("%u Prime: %u\n",uiNumberOfPrimesToFind,uiPrimeList[uiNumberOfPrimesToFind-1]);  
  148.  printf("Time taken: %.2lf\n\n",difftime(timeEnd,timeStart));
  149.  
  150.  #ifdef _DEBUG_
  151.  printf("Divisions necessary: %u\n\n",uiDivisions);
  152.  printf("Increments necessary: %u\n\n",uiIncrements);
  153.  #endif
  154.    
  155.  system("pause");
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement