Pannyx

devel

Request #10


~/devel/sleek
% pp primes_m.c
#include <qlist_int_chunk.h>

static void set_filename (int_t);
static bool_t is_prime (void);


static int_t n_filename;
static char * s_filename;

static FILE * f_out;
static FILE * f_in;

#define S_PATH "devel/trash"
#define S_FILE "primes_00.bin"

static const int_t m_files = 32;

static const int_t m_items = 0x0000000000002000 / sizeof (int_t);
static const int_t m_chunks = 0x0000000000040000;

static qlist_int_chunk_t chunks_q;
static qlist_int_chunk_node_t chunks_p;

static int_t a_chunk [m_items];
static int_t b_chunk [m_items];

static int_t * v_out;
static int_t * v_in;

static int_t c_sqrt;
static int_t c_square;
static int_t c_prime;
static int_t b_prime;
static int_t n_primes;
static int_t xadd_c;
static int_t xadd_d;
static int_t xadd_b;
static int_t xadd_n;


static void
set_filename (int_t n)
{
   int_t i; int_t j; int_t k;

   i = n_filename - 5; j = n;
   while (0 < j) {
      k = j & 15; if (k < 10) { k += 48; } else { k += 87; }
      s_filename[i] = k; i -= 1; j >>= 4;
   }
   return;

}

static bool_t
is_prime (void)
{
   int_t h; int_t i; int_t k; int_t n;

// L1:
   while (true) {
      chunks_p = qlist_int_chunk_to_first(chunks_q); if (chunks_p == NULL) { ouch(); goto Exit_L1; }

      v_in = qlist_int_chunk_access(chunks_p);
      k = 1;
      while (k < m_items) {
         if (c_prime % ( h = v_in[k]) == 0) { goto Exit_L1; }
         if (c_sqrt < h) { return true; }
         k += 1;
      }
      qlist_int_chunk_to_next(& chunks_p);
      while (!( chunks_p == NULL)) {
         v_in = qlist_int_chunk_access(chunks_p);
         k = 0;
         while (k < m_items) {
            if (c_prime % ( h = v_in[k]) == 0) { goto Exit_L1; }
            if (c_sqrt < h) { return true; }
            k += 1;
         }
         qlist_int_chunk_to_next(& chunks_p);
      }
      v_in = b_chunk;
      i = 1;
      while (true) {
         set_filename(i);
         if (( f_in = fopen(s_filename, "rb")) == NULL) { ouch(); goto Exit_L1; }

         n = 0;
         while (n < m_chunks) {
            if (fread(v_in, sizeof (int_t), m_items, f_in) < m_items) { ouch(); fclose(f_in); goto Exit_L1; }

            k = 0;
            while (k < m_items) {
               if (c_prime % ( h = v_in[k]) == 0) { fclose(f_in); goto Exit_L1; }
               if (c_sqrt < h) { fclose(f_in); return true; }
               k += 1;
            }
            n += 1;
         }
         fclose(f_in); f_in = NULL;
         i += 1;
      }
   }
Exit_L1:
   return false;

}

int
main (int argc, char * argv [])
{
   int_t d; int_t h; int_t k; int_t n;
   int_t n_files;
   char * s_home;
   bool_t is_found;

   error_flag = false;
   printf("\n");
   if (( s_home = getenv("HOME")) == NULL) { ouch(); goto Exit_M2; }

   h = strlen(s_home); k = strlen(S_PATH); n = strlen(S_FILE);
   d = h + 1 + k + 1 + n + 1;
   {
      char s [d];

      n_filename = h; strncpy(s, s_home, n_filename); s [n_filename] = '/'; n_filename += 1;
      d = k; strncpy(& s [n_filename], S_PATH, d); n_filename += d; s [n_filename] = '/'; n_filename += 1;
      d = n; strncpy(& s [n_filename], S_FILE, d); n_filename += d; s [n_filename] = '\0';
      s_filename = s;
      v_in = a_chunk;
      n_primes = 0;
      n_files = 0;
      f_in = NULL;
// L1:
      while (true) {
         set_filename(n_files);
      // if (access(s_filename, R_OK) == 0) {
         if (( f_in = fopen(s_filename, "rb")) == NULL) { goto Exit_L1; }
      // } else { ouch(); goto Exit_M2; }

         n = 0;
         while (n < m_chunks) {
            if (fread(v_in, sizeof (int_t), m_items, f_in) < m_items) { ouch(); fclose(f_in); goto Exit_M2; }

            b_prime = v_in[m_items - 2];
            c_prime = v_in[m_items - 1];
            n_primes += m_items;
            n += 1;
         }
         fclose(f_in); f_in = NULL;
         n_files += 1;
      }
Exit_L1:
      chunks_q = new_qlist_int_chunk(m_items); if (error_flag) { goto Exit_M2; }

      c_sqrt = 1; c_square = 1;
      qlist_int_chunk_enqueue(chunks_q, NULL);
      if (n_files < 1) {
         printf("0x%016llx %15lld [0x%016llx %15lld]\n", int_c( 1), int_c( 1), int_c(0), int_c(0)); printf("0x%016llx %15lld [0x%016llx
   ↪ %15lld]\n\n", int_c( 1), int_c( 1), int_c(1), int_c(1));
         printf("0x%016llx %15lld [0x%016llx %15lld]\n", int_c( 2), int_c( 2), int_c(1), int_c(1)); printf("0x%016llx %15lld [0x%016llx
   ↪ %15lld]\n\n", int_c( 2), int_c( 2), int_c(2), int_c(2));
         printf("0x%016llx %15lld [0x%016llx %15lld]\n", int_c( 3), int_c( 3), int_c(2), int_c(2)); printf("0x%016llx %15lld [0x%016llx
   ↪ %15lld]\n\n", int_c( 3), int_c( 3), int_c(3), int_c(3));
         printf("0x%016llx %15lld [0x%016llx %15lld]\n", int_c( 5), int_c( 5), int_c(3), int_c(3)); printf("0x%016llx %15lld [0x%016llx
   ↪ %15lld]\n\n", int_c( 5), int_c( 5), int_c(4), int_c(4));
         printf("0x%016llx %15lld [0x%016llx %15lld]\n", int_c( 7), int_c( 7), int_c(4), int_c(4)); printf("0x%016llx %15lld [0x%016llx
   ↪ %15lld]\n\n", int_c( 8), int_c( 8), int_c(5), int_c(5));
         xadd_c = 13; xadd_d = 8; xadd_n = 6;
         v_out = qlist_int_chunk_access(qlist_int_chunk_to_last(chunks_q));
         b_prime = 2; v_out[0] = b_prime;
         c_prime = 3; v_out[1] = c_prime;
         v_in = v_out;
         d = 2;
// L2:
         while (d < m_items) {
            c_prime += 2; if (c_square < c_prime) { c_sqrt += 1; c_square = c_sqrt * c_sqrt; }
            k = 1;
// L3:
            while (true) {
               if (c_prime % ( h = v_in[k]) == 0) { goto Next_L2; }
               if (c_sqrt < h) { goto Exit_L3; }
               k += 1;
            }
Exit_L3:
            v_out[d] = c_prime;
            if (13 < c_prime && xadd_c < c_prime) { printf("0x%016llx %15lld [0x%016llx %15lld]\n", b_prime, b_prime, d, d); printf("0x%016llx
   ↪ %15lld [0x%016llx %15lld]\n\n", xadd_c, xadd_c, xadd_n, xadd_n); xadd_b = xadd_c; xadd_c += xadd_d; xadd_d = xadd_b; xadd_n += 1; }
            b_prime = c_prime;
            d += 1;
Next_L2:
            ;
         }  // L2
         n_primes = m_items;
         n = 1;
         while (n < m_chunks) {
            qlist_int_chunk_enqueue(chunks_q, NULL);
            v_out = qlist_int_chunk_access(qlist_int_chunk_to_last(chunks_q));
            d = 0;
// L4:
            while (d < m_items) {
               c_prime += 2; if (c_square < c_prime) { c_sqrt += 1; c_square = c_sqrt * c_sqrt; }
               v_in = qlist_int_chunk_access(qlist_int_chunk_to_first(chunks_q));
               k = 1;
// L5:
               while (true) {
                  if (c_prime % ( h = v_in[k]) == 0) { goto Next_L4; }
                  if (c_sqrt < h) { goto Exit_L5; }
                  k += 1;
               }
Exit_L5:
               v_out[d] = c_prime;
               if (xadd_c < c_prime) { printf("0x%016llx %15lld [0x%016llx %15lld]\n", b_prime, b_prime, n_primes - 1 + d, n_primes - 1 + d);
   ↪ printf("0x%016llx %15lld [0x%016llx %15lld]\n\n", xadd_c, xadd_c, xadd_n, xadd_n); xadd_b = xadd_c; xadd_c += xadd_d; xadd_d = xadd_b; xadd_n += 1; }
               b_prime = c_prime;
               d += 1;
Next_L4:
               ;
            }  // L3
            n += 1;
            n_primes += m_items;
         }
         set_filename(n_files);
         if (( f_out = fopen(s_filename, "wb")) == NULL) { ouch(); goto Exit_M1; }

         chunks_p = qlist_int_chunk_to_first(chunks_q);
         while (!( chunks_p == NULL)) {
            v_out = qlist_int_chunk_access(chunks_p);

            if (fwrite(v_out, sizeof (int_t), m_items, f_out) < m_items) { ouch(); fclose(f_out); goto Exit_M1; }

            qlist_int_chunk_to_next(& chunks_p);
         }
         fclose(f_out);
         n_files = 1;
      }
      else {
         while (c_square < c_prime) { c_sqrt += 1; c_square = c_sqrt * c_sqrt; }  // c_sqrt = 66199; c_square = 4382307601;
         xadd_c = 21; xadd_d = 13; xadd_n = 7;
         while (xadd_c < b_prime) { xadd_b = xadd_c; xadd_c += xadd_d; xadd_d = xadd_b; xadd_n += 1; }  // xadd_c = 4807526976; xadd_d =
   ↪ 2971215073; xadd_n = 47;
      }
// printf("%lld files\n", n_files);
// printf("0x%016llx %15lld [0x%016llx %15lld]\n", b_prime, b_prime, n_primes - 1, n_primes - 1);
// printf("0x%016llx %15lld [0x%016llx %15lld]\n", c_prime, c_prime, n_primes, n_primes);
// printf("0x%016llx %15lld, 0x%016llx %15lld\n", c_square, c_square, xadd_c, xadd_c);
      is_found = false;
      v_out = a_chunk;
      while (n_files < m_files || is_found) {
         set_filename(n_files);
         if (( f_out = fopen(s_filename, "wb")) == NULL) { ouch(); goto Exit_M1; }

         n = 0;
         while (n < m_chunks) {
            d = 0;
            while (d < m_items) {
               c_prime += 2; if (c_square < c_prime) { c_sqrt += 1; c_square = c_sqrt * c_sqrt; }
               if (is_prime()) {
                  v_out[d] = c_prime;
                  if (xadd_c < c_prime) { printf("0x%016llx %15lld [0x%016llx %15lld]\n", b_prime, b_prime, n_primes - 1 + d, n_primes - 1 + d);
   ↪ printf("0x%016llx %15lld [0x%016llx %15lld]\n\n", xadd_c, xadd_c, xadd_n, xadd_n); xadd_b = xadd_c; xadd_c += xadd_d; xadd_d = xadd_b; xadd_n += 1; }
                  b_prime = c_prime;
                  d += 1;
                  if (0x00001fffffffffff < c_prime && ! is_found) {
                     printf("0x%016llx %15lld [0x%016llx %15lld]\n", b_prime, b_prime, n_primes - 1 + d, n_primes - 1 + d);
                     printf("0x%016llx %15lld [0x%016llx %15lld]\n", c_prime, c_prime, n_primes + d, n_primes + d);
                     is_found = true;
                  }
               }
               if (error_flag) { fclose(f_out); goto Exit_M1; }

            }
            n += 1;
            if (fwrite(v_out, sizeof (int_t), m_items, f_out) < m_items) { ouch(); fclose(f_out); goto Exit_M1; }

            n_primes += m_items;
         }
         fclose(f_out);
         n_files += 1;
// printf("%lld files\n", n_files);
      }  // L4
   }
Exit_M1:
   delete_qlist_int_chunk(& chunks_q);
Exit_M2:
   printf("\n");
Exit_M3:
   if (error_flag) { exit(EXIT_FAILURE); }

   return EXIT_SUCCESS;

}

// primes_m.c
% cmk primes_m qlist_int_chunk
% ./primes_m

0x0000000000000001               1 [0x0000000000000000               0]
0x0000000000000001               1 [0x0000000000000001               1]

0x0000000000000002               2 [0x0000000000000001               1]
0x0000000000000002               2 [0x0000000000000002               2]

0x0000000000000003               3 [0x0000000000000002               2]
0x0000000000000003               3 [0x0000000000000003               3]

0x0000000000000005               5 [0x0000000000000003               3]
0x0000000000000005               5 [0x0000000000000004               4]

0x0000000000000007               7 [0x0000000000000004               4]
0x0000000000000008               8 [0x0000000000000005               5]

0x000000000000000d              13 [0x0000000000000006               6]
0x000000000000000d              13 [0x0000000000000006               6]

0x0000000000000013              19 [0x0000000000000008               8]
0x0000000000000015              21 [0x0000000000000007               7]

0x000000000000001f              31 [0x000000000000000b              11]
0x0000000000000022              34 [0x0000000000000008               8]

0x0000000000000035              53 [0x0000000000000010              16]
0x0000000000000037              55 [0x0000000000000009               9]

0x0000000000000059              89 [0x0000000000000018              24]
0x0000000000000059              89 [0x000000000000000a              10]

0x000000000000008b             139 [0x0000000000000022              34]
0x0000000000000090             144 [0x000000000000000b              11]

0x00000000000000e9             233 [0x0000000000000033              51]
0x00000000000000e9             233 [0x000000000000000c              12]

0x0000000000000175             373 [0x000000000000004a              74]
0x0000000000000179             377 [0x000000000000000d              13]

0x000000000000025f             607 [0x000000000000006f             111]
0x0000000000000262             610 [0x000000000000000e              14]

0x00000000000003d7             983 [0x00000000000000a6             166]
0x00000000000003db             987 [0x000000000000000f              15]

0x000000000000063d            1597 [0x00000000000000fb             251]
0x000000000000063d            1597 [0x0000000000000010              16]

0x0000000000000a13            2579 [0x0000000000000178             376]
0x0000000000000a18            2584 [0x0000000000000011              17]

0x0000000000001051            4177 [0x000000000000023e             574]
0x0000000000001055            4181 [0x0000000000000012              18]

0x0000000000001a6b            6763 [0x0000000000000367             871]
0x0000000000001a6d            6765 [0x0000000000000013              19]

0x0000000000002abb           10939 [0x0000000000000530            1328]
0x0000000000002ac2           10946 [0x0000000000000014              20]

0x000000000000452b           17707 [0x00000000000007f0            2032]
0x000000000000452f           17711 [0x0000000000000015              21]

0x0000000000006ff1           28657 [0x0000000000000c30            3120]
0x0000000000006ff1           28657 [0x0000000000000016              22]

0x000000000000b50f           46351 [0x00000000000012b9            4793]
0x000000000000b520           46368 [0x0000000000000017              23]

0x0000000000012509           75017 [0x0000000000001ce3            7395]
0x0000000000012511           75025 [0x0000000000000018              24]

0x000000000001da23          121379 [0x0000000000002ca0           11424]
0x000000000001da31          121393 [0x0000000000000019              25]

0x000000000002ff23          196387 [0x0000000000004517           17687]
0x000000000002ff42          196418 [0x000000000000001a              26]

0x000000000004d965          317797 [0x0000000000006b21           27425]
0x000000000004d973          317811 [0x000000000000001b              27]

0x000000000007d8b5          514229 [0x000000000000a674           42612]
0x000000000007d8b5          514229 [0x000000000000001c              28]

0x00000000000cb203          832003 [0x000000000001030c           66316]
0x00000000000cb228          832040 [0x000000000000001d              29]

0x0000000000148ac9         1346249 [0x0000000000019381          103297]
0x0000000000148add         1346269 [0x000000000000001e              30]

0x0000000000213ceb         2178283 [0x00000000000275b6          161206]
0x0000000000213d05         2178309 [0x000000000000001f              31]

0x000000000035c7d9         3524569 [0x000000000003d76c          251756]
0x000000000035c7e2         3524578 [0x0000000000000020              32]

0x00000000005704d3         5702867 [0x000000000006023d          393789]
0x00000000005704e7         5702887 [0x0000000000000021              33]

0x00000000008cccb3         9227443 [0x00000000000968c4          616644]
0x00000000008cccc9         9227465 [0x0000000000000022              34]

0x0000000000e3d1a5        14930341 [0x00000000000ebf6a          966506]
0x0000000000e3d1b0        14930352 [0x0000000000000023              35]

0x0000000001709e73        24157811 [0x0000000000172379         1516409]
0x0000000001709e79        24157817 [0x0000000000000024              36]

0x000000000254701d        39088157 [0x0000000000245674         2381428]
0x0000000002547029        39088169 [0x0000000000000025              37]

0x0000000003c50e93        63245971 [0x0000000000391d21         3743009]
0x0000000003c50ea2        63245986 [0x0000000000000026              38]

0x0000000006197eab       102334123 [0x00000000005a9a34         5937716]
0x0000000006197ecb       102334155 [0x0000000000000027              39]

0x0000000009de8d5b       165580123 [0x000000000091fb60         9567072]
0x0000000009de8d6d       165580141 [0x0000000000000028              40]

0x000000000ff80c35       267914293 [0x0000000000ed41e2        15548898]
0x000000000ff80c38       267914296 [0x0000000000000029              41]

0x0000000019d699a5       433494437 [0x000000000183395b        25377115]
0x0000000019d699a5       433494437 [0x000000000000002a              42]

0x0000000029cea5cd       701408717 [0x000000000279038e        41485198]
0x0000000029cea5dd       701408733 [0x000000000000002b              43]

0x0000000043a53f57      1134903127 [0x00000000040aeeb4        67825332]
0x0000000043a53f82      1134903170 [0x000000000000002c              44]

0x000000006d73e553      1836311891 [0x00000000069af4b8       110818488]
0x000000006d73e55f      1836311903 [0x000000000000002d              45]

0x00000000b11924e1      2971215073 [0x000000000ac7fc32       180878386]
0x00000000b11924e1      2971215073 [0x000000000000002e              46]

0x000000011e8d0a2f      4807526959 [0x000000001123a5a9       287548841]
0x000000011e8d0a40      4807526976 [0x000000000000002f              47]

0x00000001cfa62f0d      7778742029 [0x0000000018fde7bb       419293115]
0x00000001cfa62f21      7778742049 [0x0000000000000030              48]

0x00000002ee333947     12586268999 [0x00000000256eb37c       628011900]
0x00000002ee333961     12586269025 [0x0000000000000031              49]

0x00000004bdd96867     20365011047 [0x0000000039264503       958809347]
0x00000004bdd96882     20365011074 [0x0000000000000032              50]

0x00000007ac0ca1cf     32951280079 [0x000000005869b483      1483322499]
0x00000007ac0ca1e3     32951280099 [0x0000000000000033              51]

0x0000000c69e60a5f     53316291167 [0x000000008a00ec5b      2315316315]
0x0000000c69e60a65     53316291173 [0x0000000000000034              52]

0x0000001415f2ac3f     86267571263 [0x00000000d8b21442      3635549250]
0x0000001415f2ac48     86267571272 [0x0000000000000035              53]

0x000000207fd8b6a1    139583862433 [0x00000001559d315a      5731332442]
0x000000207fd8b6ad    139583862445 [0x0000000000000036              54]


% make clean
%

Like that, I've found out that the limits for me were reached after a runtime of eleven days, by creating 32 files.  That's because of the 1 TB available for my Mac Studio, more than 700 GB are used for caching the recordings of → my fiancee, which I have moved to cloud space from my iMac.  It seems that like that, they can be used for watching, which is much more convenient than having them only as backup on my external drive.  Even though it shows that more than 100 GB are still left, somehow, it was exactly this, 32 times 2 GB, which was possible.  Knowing this, it is ok that I used the help of a convenient program to have, too; from the Mac Ports.  And knowing that I could have gotten that far, too, by using a new Big Mac with an almost empty hard drive by letting it run using the already created files within one more week or so.  Obvioulsy they have stored them somewhere, because the results are presented immediately.  Like that, I have now all the wanted range covered, up to 2**45; yay!


% primesieve 0x000000207fd8b5ad -d256 -p -q
…
139583862433
% primesieve 0x0000003495cb61f5 -d256 -p -q
…
225851433683
% primesieve 0x0000005515a418a2 -d256 -p -q
…
365435296133
% primesieve 0x00000089ab6f7b97 -d256 -p -q
…
591286729843
% primesieve 0x000000dec1139539 -d256 -p -q
…
956722026011
% primesieve 0x000001686c8311d0 -d256 -p -q
…
1548008755873
% primesieve 0x000002472d96a809 -d256 -p -q
…
2504730781913
% primesieve 0x000003af9a19bad9 -d256 -p -q
…
4052739537877
% primesieve 0x000005f6c7b063e2 -d256 -p -q
…
6557470319797
% primesieve 0x000009a661ca1fbb -d256 -p -q
…
10610209857691
% primesieve 0x00000f9d297a849d -d256 -p -q
…
17167680177511
% primesieve 0x000019438b44a558 -d256 -p -q
…
27777890035261
% pim
>>> "0x{0:016x}".format( 139583862433)
'0x000000207fd8b6a1'
>>> "0x{0:016x}".format( 225851433683)
'0x0000003495cb62d3'
>>> "0x{0:016x}".format( 365435296133)
'0x0000005515a41985'
>>> "0x{0:016x}".format( 591286729843)
'0x00000089ab6f7c73'
>>> "0x{0:016x}".format( 956722026011)
'0x000000dec113961b'
>>> "0x{0:016x}".format( 1548008755873)
'0x000001686c8312a1'
>>> "0x{0:016x}".format( 2504730781913)
'0x000002472d96a8d9'
>>> "0x{0:016x}".format( 4052739537877)
'0x000003af9a19bbd5'
>>> "0x{0:016x}".format( 6557470319797)
'0x000005f6c7b064b5'
>>> "0x{0:016x}".format( 10610209857691)
'0x000009a661ca209b'
>>> "0x{0:016x}".format( 17167680177511)
'0x00000f9d297a8567'
>>> "0x{0:016x}".format( 27777890035261)
'0x000019438b44a63d'
>>> "0x{:010x}".format( 0x0000200000000000-0x000019438b44a63d)
'0x000006bc74bb59c3'
>>> ^D
%