Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 8th, 2020 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.cpp
  3. //  Lab04_ulam
  4. //
  5. //  Created by Paweł Kalicki on 08/04/2020.
  6. //  Copyright © 2020 Paweł Kalicki. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <thread>
  13. #include <vector>
  14. #include <chrono>
  15.  
  16. #define thread_num 4
  17. #define N 2048
  18.  
  19. using namespace std;
  20.  
  21. static unsigned char color [N][N][3];
  22.  
  23. chrono::duration<double> elapsed_seconds;
  24.  
  25. int ulam_get_map(int x, int y, int n)
  26. {
  27.     x -= (n - 1) / 2;
  28.     y -= n / 2;
  29.  
  30.     int mx = abs(x), my = abs(y);
  31.     int l = 2 * max(mx, my);
  32.     int d = y >= x ? l * 3 + x + y : l - x - y;
  33.  
  34.     return pow(l - 1, 2) + d;
  35. }
  36.  
  37. int isprime(int n)
  38. {
  39.     int p;
  40.  
  41.     for (p = 2; p*p <= n; p++)
  42.         if (n%p == 0) return 0;
  43.  
  44.     return n > 2;
  45. }
  46.  
  47. int main() {
  48.     FILE * fp;
  49.     char const *filename="ulam.ppm";
  50.     char const *comment="# ";
  51.    
  52.     int i, start_width, stop_width, start_height, stop_height;
  53.    
  54.     vector<thread> workers;
  55.    
  56.     for (i=0; i < thread_num; i++)
  57.     {
  58.         start_width = (i%2)*N/(thread_num/2);
  59.         stop_width = start_width + N/2;
  60.         start_height = floor(i/2)*N/(thread_num/2);
  61.         stop_height = start_height + N/2;
  62.        
  63.         workers.push_back(thread([start_width, stop_width, start_height, stop_height]()
  64.         {
  65.             chrono::system_clock::time_point start = chrono::system_clock::now();
  66.             int i,j,k;
  67.             for (j=start_width; j<stop_width; j++)
  68.                 for(k=start_height; k<stop_height; k++)
  69.                     if (isprime(N*N - ulam_get_map(k,j, N)))
  70.                         for (i=0;i<3;i++)
  71.                             color[j][k][i] = 255;
  72.             chrono::system_clock::time_point end = chrono::system_clock::now();
  73.             elapsed_seconds += end - start;
  74.         }));
  75.     }
  76.  
  77.     for_each(workers.begin(), workers.end(), [](thread &t)
  78.     {
  79.         t.join();
  80.     });
  81.    
  82.     fp = fopen(filename,"wb");
  83.     fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,N,N,255);
  84.     fwrite(color,1,3*N*N,fp);
  85.     fclose(fp);
  86.    
  87.     cout << "elapsed time ulama's spiral: " << elapsed_seconds.count() << '\n';
  88.    
  89.     return 0;
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top