February 03

On Monday, 3 February 2025 at 00:04:17 UTC, Sergey wrote:

>

On Sunday, 2 February 2025 at 22:40:41 UTC, Jabari Zakiya wrote:

>

I am really impressed!
D is phenomenally memory efficient with this code.
I just ran the D code for some really large n values.
On my older Lenovo Legion 5, using Linux (June 2024) w/16GB
I was able to go up to n = 1,000,000,0000 and it took up ~14.4GB max, out of ~14.9GB usable.
I got a new, fast TUXEDO Gen 3, w/64GB (Jan 2025), so I'm going to how high I can go on it.

Nice. Maybe create a github repo with results from different languages. It will be interesting to see the comparison.

I haven't checked other parts of the code - maybe it is possible to improve the performance a bit more (to reduce some allocations, use appender instead of ~= operator)

But will see what other will produce first :)

Good idea. I'll see if I'm up to it.

Another code opt for you.

I translated this Ruby code:

  lhr_del = lhr_mults.map { |r_del| (r_del > ndiv2 ? n - r_del : r_del) }

  lhr_rmax = Integer.sqrt(n-2) + 2                 # the lhr_rmax high bound
  lhr_rmax_cnt = lhr.count { |r| r <= lhr_rmax }   # count of lhr <= lhr_rmax
  lhr -= lhr_del                                   # lhr pcp prime residues
  pcp_rmax = lhr.select { |r| r if r <= lhr_rmax } # lhr pcp prime residues <= lhr_rmax

To this D code. It works, seems fast, can it be done shorter, faster, more idiomatic?

  auto lhr_del = lhr_mults.map!((r_del) => r_del > ndiv2 ? n - r_del : r_del).array;
  lhr_del.sort!("a < b");

  auto lhr_rmax = 2 + cast(uint) sqrt(cast(double) n-2);
  auto lhr_rmax_cnt = 0;
  foreach(r; lhr) { if (r > lhr_rmax) break; lhr_rmax_cnt += 1;}
  lhr = setDifference(lhr, lhr_del).array;
  uint[] pcp_rmax;
  foreach(pcp; lhr) { if (pcp > lhr_rmax) break; pcp_rmax ~= pcp; }
February 03

On Monday, 3 February 2025 at 04:15:09 UTC, Jabari Zakiya wrote:

>

I translated this Ruby code:

To this D code. It works, seems fast, can it be done shorter, faster, more idiomatic?

translated code isnt going to be idiomatic ever; just state what you want done

February 04

On Monday, 3 February 2025 at 04:59:43 UTC, monkyyy wrote:

>

On Monday, 3 February 2025 at 04:15:09 UTC, Jabari Zakiya wrote:

>

I translated this Ruby code:

FYI.
This code finds all the prime pairs that sum to n for all even integers n > 2.
It (Ruby code) is included in a paper I just released (Feb 2, 2025) on Goldbach's Conjecture (1742) and Bertrand's Postulate (1845). For those interested in Prime|Number Theory check it out. A lot of new, good, stuff is in it!

Proof of Goldbach's Conjecture and Bertrand's Postulate Using Prime Generator Theory (PGT)

https://www.academia.edu/127404211/Proof_of_Goldbachs_Conjecture_and_Bertrands_Postulate_Using_Prime_Generator_Theory_PGT_

February 05

On Tuesday, 4 February 2025 at 17:17:42 UTC, Jabari Zakiya wrote:

>

On Monday, 3 February 2025 at 04:59:43 UTC, monkyyy wrote:

>

On Monday, 3 February 2025 at 04:15:09 UTC, Jabari Zakiya wrote:

>

I translated this Ruby code:

FYI.
This code finds all the prime pairs that sum to n for all even integers n > 2.
It (Ruby code) is included in a paper I just released (Feb 2, 2025) on Goldbach's Conjecture (1742) and Bertrand's Postulate (1845). For those interested in Prime|Number Theory check it out. A lot of new, good, stuff is in it!

Proof of Goldbach's Conjecture and Bertrand's Postulate Using Prime Generator Theory (PGT)

https://www.academia.edu/127404211/Proof_of_Goldbachs_Conjecture_and_Bertrands_Postulate_Using_Prime_Generator_Theory_PGT_

I updated the code to make entering data easier.
It now mimics the Ruby|Crystal code and takes "humanized" data strings.
You can now run the code as: $ ./primes_pair_lohi_u32 123_456_780

I also created two versions, one for u32 input values, and one for u64.
Unless you have lots of memmory, the u32 version is best to use.

Here's the u32 input size version.

// Compile with ldc2: $ ldc2 --release -O3 -mcpu native prime_pairs_lohi_u32.d
// Run as: $ ./prime_pairs_lohi_u32 123_456_780

module prime_pairs;

import std;
import std.datetime.stopwatch : StopWatch;

void prime_pairs_lohi(uint n) {   // inputs can be of size u32
  if ((n&1) == 1 || n < 4) { return writeln("Input not even n > 2"); }
  if (n <= 6) { writeln([n, 1]); writeln([n/2, n/2]); writeln([n/2, n/2]); return; }

  // generate the low-half-residues (lhr) r < n/2
  auto ndiv2 = n/2;               // llr:hhr midpoint
  auto rhi   = n-2;               // max residue limit
  uint[] lhr = iota(3, ndiv2, 2).filter!(e => gcd(e, n) == 1).array;

  // store all the powers of the lhr members < n-2
  uint[] lhr_mults;               // for lhr values not part of a pcp
  foreach(r; lhr) {               // step thru the lhr members
    auto r_pwr = r;               // set to first power of r
    if (r > rhi/r_pwr) break;     // exit if r^2 > n-2, as all others are too
    while (r < rhi/r_pwr)         // while r^e < n-2
      lhr_mults ~=(r_pwr *= r);   // store its current power of rA
  }

  // store all the cross-products of the lhr members < n-2
  foreach(i, r; lhr) {
    auto ri_max = rhi / r;        // ri can't multiply r with values > this
    if (lhr[i+1] > ri_max) break; // exit if product of consecutive r’s > n-2
    foreach(ri; lhr[i+1..$]) {    // for each residue in reduced list
      if (ri > ri_max) break;     // exit for r if cross-product with ri > n-2
      lhr_mults ~= r * ri;        // store value if < n-2
  } }

  // convert lhr_mults vals > n/2 to their lhr complements n-r,
  // store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
  auto lhr_del = lhr_mults.map!((r_del) => r_del > ndiv2 ? n - r_del : r_del).array;
  lhr_del.sort!("a < b");
  lhr = setDifference(lhr, lhr_del).array;

  writeln([n,     lhr.length]);   // show n and pcp prime pairs count
  writeln([lhr[0],  n-lhr[0]]);   // show first pcp prime pair of n
  writeln([lhr[$-1],n-lhr[$-1]]); // show last  pcp prime pair of n
}

void main(string[] args) {        // directly recieve input from terminal
  string[] inputs = args[1..$];   // can include '_': 123_456
  auto nums = inputs.map!(i => i.filter!(n => n != '_'));
  auto n    = nums.map!(f => f.to!uint())[0];

  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}

Here's the u64 version.

// Compile with ldc2: $ ldc2 --release -O3 -mcpu native prime_pairs_lohi_u64.d
// Run as: $ ./prime_pairs_lohi_u64 123_456_780

module prime_pairs;

import std;
import std.datetime.stopwatch : StopWatch;

void prime_pairs_lohi(ulong n) {  // inputs can be of size u64
  if ((n&1) == 1 || n < 4) { return writeln("Input not even n > 2"); }
  if (n <= 6) { writeln([n, 1]); writeln([n/2, n/2]); writeln([n/2, n/2]); return; }

  // generate the low-half-residues (lhr) r < n/2
  auto ndiv2 = n/2;               // llr:hhr midpoint
  auto rhi   = n-2;               // max residue limit
  ulong[] lhr = iota(3, ndiv2, 2).filter!(e => gcd(e, n) == 1).array;

  // store all the powers of the lhr members < n-2
  ulong[] lhr_mults;              // for lhr values not part of a pcp
  foreach(r; lhr) {               // step thru the lhr members
    auto r_pwr = r;               // set to first power of r
    if (r > rhi/r_pwr) break;     // exit if r^2 > n-2, as all others are too
    while (r < rhi/r_pwr)         // while r^e < n-2
      lhr_mults ~=(r_pwr *= r);   // store its current power of rA
  }

  // store all the cross-products of the lhr members < n-2
  foreach(i, r; lhr) {
    auto ri_max = rhi / r;        // ri can't multiply r with values > this
    if (lhr[i+1] > ri_max) break; // exit if product of consecutive r’s > n-2
    foreach(ri; lhr[i+1..$]) {    // for each residue in reduced list
      if (ri > ri_max) break;     // exit for r if cross-product with ri > n-2
      lhr_mults ~= r * ri;        // store value if < n-2
  } }

  // convert lhr_mults vals > n/2 to their lhr complements n-r,
  // store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
  auto lhr_del = lhr_mults.map!((r_del) => r_del > ndiv2 ? n - r_del : r_del).array;
  lhr_del.sort!("a < b");
  lhr = setDifference(lhr, lhr_del).array;

  writeln([n,     lhr.length]);   // show n and pcp prime pairs count
  writeln([lhr[0],  n-lhr[0]]);   // show first pcp prime pair of n
  writeln([lhr[$-1],n-lhr[$-1]]); // show last  pcp prime pair of n
}

void main(string[] args) {        // directly recieve input from terminal
  string[] inputs = args[1..$];   // can include '_': 123_456
  auto nums = inputs.map!(i => i.filter!(n => n != '_'));
  auto n    = nums.map!(f => f.to!ulong())[0];

  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}
February 08

On Wednesday, 5 February 2025 at 21:24:51 UTC, Jabari Zakiya wrote:

>

On Tuesday, 4 February 2025 at 17:17:42 UTC, Jabari Zakiya wrote:

>

On Monday, 3 February 2025 at 04:59:43 UTC, monkyyy wrote:

>

On Monday, 3 February 2025 at 04:15:09 UTC, Jabari Zakiya wrote:

>

I translated this Ruby code:

FYI.
This code finds all the prime pairs that sum to n for all even integers n > 2.
It (Ruby code) is included in a paper I just released (Feb 2, 2025) on Goldbach's Conjecture (1742) and Bertrand's Postulate (1845). For those interested in Prime|Number Theory check it out. A lot of new, good, stuff is in it!

Proof of Goldbach's Conjecture and Bertrand's Postulate Using Prime Generator Theory (PGT)

https://www.academia.edu/127404211/Proof_of_Goldbachs_Conjecture_and_Bertrands_Postulate_Using_Prime_Generator_Theory_PGT_

I updated the code to make entering data easier.
It now mimics the Ruby|Crystal code and takes "humanized" data strings.
You can now run the code as: $ ./primes_pair_lohi_u32 123_456_780

I also created two versions, one for u32 input values, and one for u64.
Unless you have lots of memmory, the u32 version is best to use.

Here's the u32 input size version.

// Compile with ldc2: $ ldc2 --release -O3 -mcpu native prime_pairs_lohi_u32.d
// Run as: $ ./prime_pairs_lohi_u32 123_456_780

module prime_pairs;

import std;
import std.datetime.stopwatch : StopWatch;

void prime_pairs_lohi(uint n) {   // inputs can be of size u32
  if ((n&1) == 1 || n < 4) { return writeln("Input not even n > 2"); }
  if (n <= 6) { writeln([n, 1]); writeln([n/2, n/2]); writeln([n/2, n/2]); return; }

  // generate the low-half-residues (lhr) r < n/2
  auto ndiv2 = n/2;               // llr:hhr midpoint
  auto rhi   = n-2;               // max residue limit
  uint[] lhr = iota(3, ndiv2, 2).filter!(e => gcd(e, n) == 1).array;

  // store all the powers of the lhr members < n-2
  uint[] lhr_mults;               // for lhr values not part of a pcp
  foreach(r; lhr) {               // step thru the lhr members
    auto r_pwr = r;               // set to first power of r
    if (r > rhi/r_pwr) break;     // exit if r^2 > n-2, as all others are too
    while (r < rhi/r_pwr)         // while r^e < n-2
      lhr_mults ~=(r_pwr *= r);   // store its current power of rA
  }

  // store all the cross-products of the lhr members < n-2
  foreach(i, r; lhr) {
    auto ri_max = rhi / r;        // ri can't multiply r with values > this
    if (lhr[i+1] > ri_max) break; // exit if product of consecutive r’s > n-2
    foreach(ri; lhr[i+1..$]) {    // for each residue in reduced list
      if (ri > ri_max) break;     // exit for r if cross-product with ri > n-2
      lhr_mults ~= r * ri;        // store value if < n-2
  } }

  // convert lhr_mults vals > n/2 to their lhr complements n-r,
  // store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
  auto lhr_del = lhr_mults.map!((r_del) => r_del > ndiv2 ? n - r_del : r_del).array;
  lhr_del.sort!("a < b");
  lhr = setDifference(lhr, lhr_del).array;

  writeln([n,     lhr.length]);   // show n and pcp prime pairs count
  writeln([lhr[0],  n-lhr[0]]);   // show first pcp prime pair of n
  writeln([lhr[$-1],n-lhr[$-1]]); // show last  pcp prime pair of n
}

void main(string[] args) {        // directly recieve input from terminal
  string[] inputs = args[1..$];   // can include '_': 123_456
  auto nums = inputs.map!(i => i.filter!(n => n != '_'));
  auto n    = nums.map!(f => f.to!uint())[0];

  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}

I've updated the code to make it shorter|simpler.

module prime_pairs;

import std;
import std.datetime.stopwatch : StopWatch;

void prime_pairs_lohi(uint n) {   // inputs can be of size u32
  if ((n&1) == 1 || n < 4) { return writeln("Input not even n > 2"); }
  if (n <= 6) { writeln([n, 1]); writeln([n/2, n/2]); writeln([n/2, n/2]); return; }

  // generate the low-half-residues (lhr) r < n/2
  auto ndiv2 = n/2;               // llr:hhr midpoint
  auto rhi   = n-2;               // max residue limit
  uint[] lhr = iota(3, ndiv2, 2).filter!(e => gcd(e, n) == 1).array;

  // store all powers and cross-products of the lhr members < n-2
  uint[] lhr_mults;               // lhr multiples, not part of a pcp
  foreach(i, r; lhr) {
    auto rmax = rhi / r;          // ri can't multiply r with values > this
    if (r < rmax) lhr_mults ~= r*r; // for r^2 multiples
    if (lhr[i+1] > rmax) break  ; // exit if product of consecutive r’s > n-2
    foreach(ri; lhr[i+1..$]) {    // for each residue in reduced list
      if (ri > rmax) break;       // exit for r if cross-product with ri > n-2
      lhr_mults ~= r * ri;        // store value if < n-2
  } }

  // convert lhr_mults vals > n/2 to their lhr complements n-r,
  // store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
  auto lhr_del = lhr_mults.map!((r_del) => r_del > ndiv2 ? n - r_del : r_del).array;
  lhr_del.sort!("a < b");
  lhr = setDifference(lhr, lhr_del).array; // lhr now contains just pcp primes

  writeln([n,     lhr.length]);   // show n and pcp prime pairs count
  writeln([lhr[0],  n-lhr[0]]);   // show first pcp prime pair of n
  writeln([lhr[$-1],n-lhr[$-1]]); // show last  pcp prime pair of n
}

void main(string[] args) {        // directly recieve input from terminal
  string[] inputs = args[1..$];   // can include '_': 123_456
  auto nums = inputs.map!(i => i.filter!(n => n != '_'));
  auto n    = nums.map!(f => f.to!uint())[0];

  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}
1 2
Next ›   Last »