Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - scomposizione numeri primi
Forum - C/C++ - scomposizione numeri primi - Pagina 2

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
HeDo (Founder Member)
Guru^2


Messaggi: 2765
Iscritto: 21/09/2007

Segnala al moderatore
Postato alle 16:50
Lunedì, 23/11/2009
Testo quotato

Postato originariamente da pierotofy:

Io un programma simile non l'ho mai fatto... mi sarebbe piaciuto vedere come e' stato risolto.



vabbè se deve venire piero a chiedermelo :asd:

Codice sorgente - presumibilmente C++

  1. #include <iostream>
  2. #include <windows.h>
  3. #include <math.h>
  4.  
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. bool bDone = false;
  11. DWORD dwTime;
  12. ULONG n;
  13. ULONG ulMax;
  14.  
  15. // Delay between updates
  16. const int DELAY = 1000;
  17.  
  18. // Async procedure to show progress
  19. DWORD WINAPI ThreadProc(LPVOID lpParameter) {
  20.  
  21.         DWORD dwTempTime;
  22.         ULONG ulNumber;
  23.         ULONG ulLastNumber = 3;
  24.  
  25.         HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  26.  
  27.         COORD cCoord;
  28.  
  29.         cCoord.X = 0;
  30.         cCoord.Y = 20;
  31.  
  32.         do {
  33.  
  34.                
  35.                 dwTempTime = GetTickCount() - dwTime;
  36.                 ulNumber = n;
  37.  
  38.                 ULONG ulDiff = ulNumber - ulLastNumber;
  39.  
  40.                 float fPerc = ((float)ulNumber / ulMax) * 100;
  41.  
  42.                 SetConsoleCursorPosition(hOut, cCoord);
  43.  
  44.                 cout << "Time : " << dwTempTime << " ms" << endl;
  45.                 cout << "Number: " << ulNumber << "/" << ulMax << " (" << fPerc << "%)" << endl;
  46.                 cout << "Speed: " << (float)ulDiff / DELAY << " numbers/msec" << endl << endl;
  47.  
  48.                 ulLastNumber = ulNumber;
  49.  
  50.                 Sleep(DELAY);
  51.  
  52.         } while(!bDone);
  53.  
  54.  
  55.         return 0;
  56.  
  57.  
  58. }
  59.  
  60. int main(int argc, char *argv[]) {
  61.  
  62.  
  63.         cout << " *** PrimeFinder by HeDo ***" << endl << endl;
  64.  
  65.         cout << "Max prime number: ";
  66.         cin >> ulMax;
  67.  
  68.         // Vector containing the prime numbers found
  69.         vector<ULONG> lstPrime;
  70.  
  71. #ifdef _EDEBUG
  72.  
  73.         cout << endl;
  74.  
  75.         cout << "Capacity: " << lstPrime.capacity() << endl;
  76.  
  77.         cout << endl << " -> After reserve() " << endl << endl;
  78.  
  79. #endif
  80.  
  81.         // Let's reserve enough space
  82.         lstPrime.reserve(ulMax);
  83.  
  84. #ifdef _EDEBUG
  85.  
  86.         lstNoise.reserve(ulMax);
  87.  
  88.         cout << "Capacity: " << lstPrime.capacity() << endl;
  89.  
  90. #endif
  91.  
  92.         cout << endl;
  93.                
  94.         // The 2 and 3 are added this way coz of optimization
  95.         lstPrime.push_back(2);
  96.         lstPrime.push_back(3);
  97.  
  98.         DWORD dwID;
  99.  
  100.         // Create and start the monitor thread
  101.         HANDLE hTh = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwID);
  102.  
  103.         // Let's measure the time
  104.         dwTime = GetTickCount();
  105.  
  106.         bool bPrime;
  107.  
  108.         // We can skip the even number
  109.         for (n = 3; n < ulMax; n += 2) {
  110.                                
  111.  
  112. #ifdef _EDEBUG
  113.  
  114.                 cout << "Checking number " << n << endl;
  115.  
  116. #endif
  117.  
  118.                 bPrime = true;
  119.  
  120.                 // This method is called "factorization test", it's the slowest but the simpliest to implement :)
  121.                 for (ULONG h = 0; h < lstPrime.size(); h++) {
  122.                                        
  123.  
  124. #ifdef _EDEBUG
  125.                         cout << "Selected " << lstPrime[h] << endl;
  126. #endif
  127.  
  128.                         // We dont need to test prims above the square root of the number
  129.                         if (lstPrime[h] > sqrt((double)n)) {
  130.  
  131. #ifdef _EDEBUG
  132.                                 cout << "Breaking: " << lstPrime[h] << " is higher than n / 3 = " << n / 3 << endl;
  133. #endif
  134.  
  135.                                 break;
  136.  
  137.                         }
  138.  
  139. #ifdef _EDEBUG
  140.                         cout << "Real prime check: " << n << " % " << lstPrime[h] << " = " << n % lstPrime[h] << endl;
  141. #endif
  142.  
  143.                         // Remainder check
  144.                         if (n % lstPrime[h] == 0) {
  145.  
  146. #ifdef _EDEBUG
  147.                                 cout << " -> Not prime number " << endl;
  148. #endif
  149.  
  150.                                 bPrime = false;
  151.  
  152.                                 break;
  153.  
  154.                         }
  155.  
  156.  
  157.                 }
  158.  
  159.                 // If it is prime
  160.                 if (bPrime) {
  161.  
  162. #ifdef _EDEBUG
  163.  
  164.                         cout << " !> " << n << " is prime" << endl;
  165. #endif
  166.                         // Add it to our vector
  167.                         lstPrime.push_back(n);
  168.  
  169.                 }
  170.  
  171.         }
  172.  
  173.         // How long did it take?
  174.         dwTime = GetTickCount() - dwTime;
  175.  
  176.         bDone = true;
  177.  
  178.         // "out.txt" contains the prime numbers found
  179.         ofstream out("out.txt", ios::out);
  180.  
  181.         // "noise.csv" contains the deltas between the primes
  182.         ofstream noise("noise.csv", ios::out);
  183.  
  184.         out << lstPrime[0] << ", ";
  185.  
  186.         // Write the files
  187.         for(int n = 1; n < lstPrime.size(); n++) {
  188.  
  189.                 out << lstPrime[n] << ", ";
  190.  
  191.                 noise << (lstPrime[n] - lstPrime[n - 1]) << "; ";
  192.  
  193.         }
  194.  
  195.         out.close();
  196.         noise.close();
  197.  
  198.         // Performance check
  199.         cout << endl << "Time: " << dwTime << "ms, for " << lstPrime.size() << " numbers";
  200.  
  201.         cout << endl << endl;
  202.  
  203.         system("PAUSE");
  204.  
  205.         return 0;
  206. }
  207.  
  208. /*
  209.  
  210. x86
  211.  
  212.  *** PrimeFinder by HeDo ***
  213.  
  214. Max prime number: 1000000
  215.  
  216. Time: 8844ms, for 78498 numbers
  217.  
  218. x64
  219.  
  220.  *** PrimeFinder by HeDo ***
  221.  
  222. Max prime number: 1000000
  223.  
  224. Time: 9078ms, for 78498 numbers
  225.  
  226. intel x86
  227.  
  228.  *** PrimeFinder by HeDo ***
  229.  
  230. Max prime number: 1000000
  231.  
  232. Time: 8829ms, for 78498 numbers
  233.  
  234. intel x86
  235.  
  236.  *** PrimeFinder by HeDo ***
  237.  
  238. Max prime number: 1000000
  239.  
  240.  
  241. Time: 8843ms, for 78498 numbers
  242.  
  243. */


PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo