Jump to page: 1 2 3
Thread overview
help with prime pairs code
Feb 01
user1234
Feb 02
user1234
Feb 02
user1234
Feb 02
Sergey
Feb 03
Sergey
Feb 03
monkyyy
5 days ago
Jabari Zakiya
5 days ago
monkyyy
5 days ago
Salih Dincer
4 days ago
Sergey
4 days ago
Salih Dincer
2 days ago
Salih Dincer
January 31

I'm converting Ruby code to D and am running into issues.
Would appreciate what needs to be done to get it to compile/run correctly.

Here's the Ruby code:

# Enable YJIT if using CRuby >= 3.3"
RubyVM::YJIT.enable if RUBY_ENGINE == "ruby" and RUBY_VERSION.to_f >= 3.3

def prime_pairs_lohi(n)
  return puts "Input not even n > 2" unless n.even? && n > 2
  return (pp [n, 1]; pp [n/2, n/2]; pp [n/2, n/2]) if n <= 6

  # generate the low-half-residues (lhr) r < n/2
  lhr = 3.step(n/2, 2).select { |r| r if r.gcd(n) == 1 }
  ndiv2, rhi = n/2, n-2            # lhr:hhr midpoint, max residue limit
  lhr_mults = []                   # for lhr values not part of a pcp

  # store all the powers of the lhr members < n-2
  lhr.each do |r|                  # step thru the lhr members
    r_pwr = r                      # set to first power of r
    break if r > rhi / r_pwr       # 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 r
    end
  end

  # store all the cross-products of the lhr members < n-2
  lhr_dup = lhr.dup                # make copy of the lhr members list
  while (r = lhr_dup.shift) && !lhr_dup.empty? # do mults of 1st list r w/others
    ri_max = rhi / r               # ri can't multiply r with values > this
    break if lhr_dup[0] > ri_max   # exit if product of consecutive r’s > n-2
    lhr_dup.each do |ri|           # for each residue in reduced list
      break if ri > ri_max         # exit for r if cross-product with ri > n-2
      lhr_mults << r * ri          # store value if < n-2
    end                            # check cross-products of next lhr member
  end

  # 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
  lhr_del = lhr_mults.map { |r_del| (r_del > ndiv2 ? n - r_del : r_del) }

  pp [n, (lhr -= lhr_del).size]    # show n and pcp prime pairs count
  pp [lhr.first, n-lhr.first]      # show first pcp prime pair of n
  pp [lhr.last,  n-lhr.last]       # show last  pcp prime pair of n
end

def tm; t = Time.now; yield; Time.now - t end  # to time runtime execution

n = ARGV[0].to_i                   # get n value from terminal
puts tm { prime_pairs_lohi(n) }    # show execution runtime as last output

Here's my D code.

module prime_pairs;
import std;

//import std.datetime.stopwatch : StopWatch;
//import std.stdio : readf, write, writeln, readln;
//import std.numeric : gcd;

void prime_pairs_lohi(uint n)  {
  if ((n|1) == 1 || n < 4) return writeln("Input not even n > 2");
  //if (n <= 6) return { writel([n, 1]); writeln([n/2, n/2]); writeln([n/2, n/2])}

  // generate the low-half-residues (lhr) r < n/2
  lhr = [];
  auto ndiv2 = n/2;
  auto rhi = n-2;
  auto r = 3;
  while (r < ndiv2) {
     if (gcd(r,n) == 1) lhr ~= r;
     r += 2;
  }
  lhr_mults = [];                  // for lhr values not part of a pcp

  // store all the powers of the lhr members < n-2
  foreach(r; lhr) {                // step thru the lhr members
    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 r
  } }

  // store all the cross-products of the lhr members < n-2
  lhr_dup = lhr.dup;               // make copy of the lhr members list
  while ((r = lhr_dup.shift) && lhr_dup.length != 0) {
    ri_max = rhi / r;              // ri can't multiply r with values > this
    if (lhr_dup[0] > ri_max) break; // exit if product of consecutive r’s > n-2
    foreach(ri; lhr_dup) {         // 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
  } }                              // check cross-products of next lhr member

  // 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
  lhr_del = lhr_mults.map { |r_del| (r_del > ndiv2 ? n - r_del : r_del) }

  lhr -= lhr_del;
  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() {
  ulong[] x;
  foreach (_; 0 .. 1) { ulong a; readf!" %d"(a); x ~= a; }
  n = x[0];
  auto stopWatchExecution = StopWatch();
  stopWatchExecution.start();
  prime_pairs_lohi(n);
  stopWatchExecution.stop();
  writeln(stopWatchExecution.peek());

February 01

On Friday, 31 January 2025 at 20:05:54 UTC, Jabari Zakiya wrote:

>

I'm converting Ruby code to D and am running into issues.
Would appreciate what needs to be done to get it to compile/run correctly.

Here's the Ruby code:

[...]

A first draft of the translation, not very idiomatic D code:

module prime_pairs;
import std;

void prime_pairs_lohi(uint n)
{
    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

    int[] lhr;
    auto ndiv2 = n/2;
    auto rhi   = n-2;

    {
        auto r = 3;
        while (r < ndiv2)
        {
            if (gcd(r,n) == 1) lhr ~= r;
                r += 2;
        }
    }

    int[] lhr_mults;               // for lhr values not part of a pcp

// store all the powers of the lhr members < n-2

    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 r
    }

// store all the cross-products of the lhr members < n-2

  auto lhr_dup = lhr.dup;            // make copy of the lhr members list
  {
        auto shift(T)(ref T[] a)
        {
            if (!a.length)
                return -1;
            auto r = a[0];
            a = a[1..$];
            return r;
        }

        int r;
        while ((r = shift(lhr_dup)) != -1 && lhr_dup.length != 0)
        {
            auto ri_max = rhi / r;   // ri can't multiply r with values > this
            if (lhr_dup[0] > ri_max)
                break;               // exit if product of consecutive r’s > n-2
            foreach(ri; lhr_dup)     // 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
            }
        }                            // check cross-products of next lhr member
    }

    // 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);

    lhr = lhr.filter!(a => !lhr_del.canFind(a)).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()
{
    prime_pairs_lohi(16);
}

Results seems to match to what I see in TryRubyPlayGround.

Summary of the problems I've seen in you attempt

  • problem of scoping (e.g loop variables)
  • missing storage class to declare variables
  • some missing 1:1 equivalent of Ruby functs .shift and the - operator on array.
  • lambda syntax not mastered (lhr_del mapping)

A bit of learning required but should be to overcome.

Now I wait for the person who will post a beautiful version with UFCS chains...

February 01

On Saturday, 1 February 2025 at 00:21:22 UTC, user1234 wrote:

>

On Friday, 31 January 2025 at 20:05:54 UTC, Jabari Zakiya wrote:

>

[...]

A first draft of the translation, not very idiomatic D code:

module prime_pairs;
import std;

[...]

Thank you very much!

As you can see, D is not my primary language, but I respect its speed and readability.
I also have a faster Crystal version, almost identical (96%) to the Rudy code.
I'm implementing it in different languages, partly as a way to learn them with a real task.

I'll study the D code thoroughly to conceptually understand the idioms used int it.
I, like you, also wait to see a "beautiful" version done.

February 02

On Saturday, 1 February 2025 at 21:56:23 UTC, Jabari Zakiya wrote:

>

On Saturday, 1 February 2025 at 00:21:22 UTC, user1234 wrote:

>

On Friday, 31 January 2025 at 20:05:54 UTC, Jabari Zakiya wrote:

>

[...]

A first draft of the translation, not very idiomatic D code:

module prime_pairs;
import std;

[...]

Thank you very much!

As you can see, D is not my primary language, but I respect its speed and readability.
I also have a faster Crystal version, almost identical (96%) to the Rudy code.

yeah Crystal is in my scope. Whyle I'm usualy not into dynamic typed langs, there's something I like in the Crystal lang.

>

I'm implementing it in different languages, partly as a way to learn them with a real task.

I'll study the D code thoroughly to conceptually understand the idioms used int it.
I, like you, also wait to see a "beautiful" version done.

February 02

On Sunday, 2 February 2025 at 01:12:59 UTC, user1234 wrote:

>

On Saturday, 1 February 2025 at 21:56:23 UTC, Jabari Zakiya wrote:

>

On Saturday, 1 February 2025 at 00:21:22 UTC, user1234 wrote:

>

On Friday, 31 January 2025 at 20:05:54 UTC, Jabari Zakiya wrote:

>

[...]

A first draft of the translation, not very idiomatic D code:

module prime_pairs;
import std;

[...]

Thank you very much!

As you can see, D is not my primary language, but I respect its speed and readability.
I also have a faster Crystal version, almost identical (96%) to the Rudy code.

yeah Crystal is in my scope. Whyle I'm usualy not into dynamic typed langs, there's something I like in the Crystal lang.

Gradual typing

That's the shit I like with Crystal.

February 02

On Sunday, 2 February 2025 at 01:39:34 UTC, user1234 wrote:

>

On Sunday, 2 February 2025 at 01:12:59 UTC, user1234 wrote:

>

On Saturday, 1 February 2025 at 21:56:23 UTC, Jabari Zakiya wrote:

>

On Saturday, 1 February 2025 at 00:21:22 UTC, user1234 wrote:

>

On Friday, 31 January 2025 at 20:05:54 UTC, Jabari Zakiya wrote:

>

[...]

A first draft of the translation, not very idiomatic D code:

module prime_pairs;
import std;

[...]

Thank you very much!

As you can see, D is not my primary language, but I respect its speed and readability.
I also have a faster Crystal version, almost identical (96%) to the Rudy code.

yeah Crystal is in my scope. Whyle I'm usualy not into dynamic typed langs, there's something I like in the Crystal lang.

Gradual typing

That's the shit I like with Crystal.

Here's the Crystal version of the Ruby code.

# Compile: crystal build --release --mcpu native prime_pairs_lohi.cr
# Run as: ./prime_pairs_lohi 123_456_780

    def prime_pairs_lohi(n)
      return puts "Input not even n > 2" unless n.even? && n > 2
      return (pp [n, 1]; pp [n//2, n//2]; pp [n//2, n//2]) if n <= 6

      # generate the low-half-residues (lhr) r < n/2
      lhr = 3u64.step(to: n//2, by: 2).select { |r| r if r.gcd(n) == 1 }.to_a
      ndiv2, rhi = n//2, n-2           # lhr:hhr midpoint, max residue limit
      lhr_mults = [] of typeof(n)      # for lhr values not part of a pcp

      # store all the powers of the lhr members < n-2
      lhr.each do |r|                  # step thru the lhr members
        r_pwr = r                      # set to first power of r
        break if r > rhi // r_pwr      # 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 r
        end
      end

      # store all the cross-products of the lhr members < n-2
      lhr_dup = lhr.dup                # make copy of the lhr members list
      while (r = lhr_dup.shift) && !lhr_dup.empty? # do mults of 1st list r w/others
        ri_max = rhi // r              # ri can't multiply r with values > this
        break if lhr_dup[0] > ri_max   # exit if product of consecutive r’s > n-2
        lhr_dup.each do |ri|           # for each residue in reduced list
          break if ri > ri_max         # exit for r if cross-product with ri > n-2
          lhr_mults << r * ri          # store value if < n-2
        end                            # check cross-products of next lhr member
      end

      # remove from lhr its lhr_mults, convert vals > n/2 to lhr complements first
      lhr -= lhr_mults.map { |r_del| r_del > ndiv2 ? n - r_del : r_del }

      pp [n, lhr.size]                 # show n and pcp prime pairs count
      pp [lhr.first, n-lhr.first]      # show first pcp prime pair of n
      pp [lhr.last,  n-lhr.last]       # show last  pcp prime pair of n
    end

def tm; t = Time.monotonic; yield; Time.monotonic - t end  # time code execution

    def gen_pcp
      n = (ARGV[0].to_u64 underscore: true) # get n input from terminal
      puts tm { prime_pairs_lohi(n) }       # show execution runtime as last output
    end

gen_pcp

Add this to the D code to take terminal input and to time execution.
I don't know if this is most idiomatic, but it works.

import std.datetime.stopwatch : StopWatch;
void main() {
  int n;
  readf("%s", &n);
  auto stopWatchExecution = StopWatch();
  stopWatchExecution.start();
  prime_pairs_lohi(n);
  stopWatchExecution.stop();
  writeln(stopWatchExecution.peek());
}

The D version is way slower, because of the array operations.
For an input of 1000000 (1M): D: 12+ secs; Crystal: 0.036 secs.

In Ruby|Crystal you can just do: lhr -= lhr_mult
to remove elements of one array from another, and not one at a time.
I assume there's a D equivalent?!?

As n increases so do those arrays, and the times become much longer.

February 02

On Sunday, 2 February 2025 at 03:22:00 UTC, Jabari Zakiya wrote:

>

The D version is way slower, because of the array operations.
For an input of 1000000 (1M): D: 12+ secs; Crystal: 0.036 secs.

First of fall make sure you are using LDC or GDC compiler. Second step is to add proper flags for optimizations (-O3).
Your initial version showed on my M1 laptop:

[1000000, 5402]
[17, 999983]
[499943, 500057]
19 secs, 367 ms, 923 μs, and 9 hnsecs

Then I propose some small changes:

  1. You can initialize lhr with 1 row in similar logic as in Ruby/Crystal
uint[] lhr = iota(3, ndiv2, 2).filter!(e => gcd(e, n) == 1).array;
  1. The logic with shift and duplicated array could be simplified in D with simple index processing and slising (you even don't need to create duplicated array)
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
        }
    }                            // check cross-products of next lhr member

But these 2 updates don't make a lot of improvements in terms of performance.

>

In Ruby|Crystal you can just do: lhr -= lhr_mult
to remove elements of one array from another, and not one at a time.
I assume there's a D equivalent?!?

In D we have setDifference, so the code instead of filter + canFind (which will be very costly in terms of big-O notation) will become

lhr_del.sort!("a < b"); // to use setDifference we need to have sorted arrays
lhr = setDifference(lhr, lhr_del).array;

So after those updates the time improved

[1000000, 5402]
[17, 999983]
[499943, 500057]
81 ms, 144 μs, and 7 hnsecs
February 02

On Sunday, 2 February 2025 at 10:26:56 UTC, Sergey wrote:

>

So after those updates the time improved

[1000000, 5402]
[17, 999983]
[499943, 500057]
81 ms, 144 μs, and 7 hnsecs

Here's the D code with the suggested changes, and other updates.
Note, I make the source code look like Ruby|Crystal for my esthetics.

// Compile with ldc2: $ ldc2 --release -O3 -mcpu native prime_pairs_lohi.d
// Run as: echo 100000 | ./prime_pairs_lohi
// Update: 2025/02/02

module prime_pairs;

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

void prime_pairs_lohi(uint n) {
  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
  int[] 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 r
  }

  // 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() {
  int n;
  readf("%s", &n);                // get input to n from terminal
  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}

The D version is now faster than Crystal.

D (LDC2 2.40)
➜  ~ echo 100000000 |./prime_pairs_lohi
[100000000, 291400]
[11, 99999989]
[49999757, 50000243]
7 secs, 422 ms, 641 μs, and 9 hnsecs

Crystal (1.15)
➜  ~ ./prime_pairs_lohi 100_000_000
[100000000, 291400]
[11, 99999989]
[49999757, 50000243]
00:00:10.454055301

I'll eventually do a Rust version, maybe C++, et al.

I want to think everybody for the help.
If you find more performance improvements please post them.

February 02

On Sunday, 2 February 2025 at 21:17:39 UTC, Jabari Zakiya wrote:

>

On Sunday, 2 February 2025 at 10:26:56 UTC, Sergey wrote:

>

So after those updates the time improved

[1000000, 5402]
[17, 999983]
[499943, 500057]
81 ms, 144 μs, and 7 hnsecs

Here's the D code with the suggested changes, and other updates.
Note, I make the source code look like Ruby|Crystal for my esthetics.

// Compile with ldc2: $ ldc2 --release -O3 -mcpu native prime_pairs_lohi.d
// Run as: echo 100000 | ./prime_pairs_lohi
// Update: 2025/02/02

module prime_pairs;

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

void prime_pairs_lohi(uint n) {
  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
  int[] 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 r
  }

  // 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() {
  int n;
  readf("%s", &n);                // get input to n from terminal
  auto timer = StopWatch();       // create execution timer
  timer.start();                  // start it
  prime_pairs_lohi(n);            // run routine
  writeln(timer.peek());          // show timer results
}

The D version is now faster than Crystal.

D (LDC2 2.40)
➜  ~ echo 100000000 |./prime_pairs_lohi
[100000000, 291400]
[11, 99999989]
[49999757, 50000243]
7 secs, 422 ms, 641 μs, and 9 hnsecs

Crystal (1.15)
➜  ~ ./prime_pairs_lohi 100_000_000
[100000000, 291400]
[11, 99999989]
[49999757, 50000243]
00:00:10.454055301

I'll eventually do a Rust version, maybe C++, et al.

I want to think everybody for the help.
If you find more performance improvements please post them.

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.

Here are some results on the Lenovo Legion 5.

➜  ~ echo 500000000 |./prime_pairs_lohi3
[500000000, 1219610]
[7, 499999993]
[249999877, 250000123]
43 secs, 402 ms, 950 μs, and 3 hnsecs
➜  ~
➜  ~ echo 900000000 |./prime_pairs_lohi3
[900000000, 4132595]
[37, 899999963]
[449999993, 450000007]
36 secs, 135 ms, 163 μs, and 6 hnsecs
➜  ~
➜  ~ echo 1000000000 |./prime_pairs_lohi3
[1000000000, 2274205]
[71, 999999929]
[499999931, 500000069]
1 minute, 32 secs, 87 ms, 624 μs, and 8 hnsecs
➜  ~
➜  ~ echo 1500000000 |./prime_pairs_lohi3
[1500000000, 6543613]
[43, 1499999957]
[749999927, 750000073]
1 minute, 6 secs, 205 ms, 381 μs, and 5 hnsecs
➜  ~
February 03

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 :)

« First   ‹ Prev
1 2 3