safety0ff:

I modified the Hamming numbers code in a personal exercise.
It now uses considerably less memory but is slower.

I've posted the code here in case it is of use: http://dpaste.dzfl.pl/3990023e5577

For a single n, n = 350_000_000:
Alternative version 2: 13.4s and ~5480 MB of ram
My code: 21s and ~74 MB of ram

I have added your version, with small changes and improvements.


I suggest to not put attributes like this:


    static struct Candidate
    {
        typeof(Hamming.ln) ln;
        typeof(Hamming.e) e;
        pure nothrow:
        void increment(size_t n)
        {
            e[n] += 1;
            ln += lnprimes[n];
        }
        const:
        bool opEquals(T)(in ref T y) {
            // return this.e == y.e; // slow
return !((this.e[0] ^ y.e[0]) | (this.e[1] ^ y.e[1]) | (this.e[2] ^ y.e[2]));
        }
        int opCmp(T)(in ref T y) {
            return ln > y.ln ? 1 : (ln < y.ln ? -1 : 0);
        }
    }

Bye,
bearophile

Reply via email to