help with prime pairs code
Jabari Zakiya
jzakiya at gmail.com
Mon Feb 3 04:15:09 UTC 2025
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; }
More information about the Digitalmars-d-learn
mailing list