June 05, 2011
It's fun to solve the euler problems at compile time, but I'm often running out of memory. Is there a way around this?

import std.stdio;

int numWithLongestChain(int max) {
    int maxIt = 0;
    int n;
    foreach(i; 1..max) {
        int it = 0;
        for(int c=i; c != 1; ++it)
            c = (c&1) == 0 ? c / 2 : c*3 + 1;
        if(it > maxIt) {
            maxIt = it;
            n = i;
        }
    }
    return n;
}
enum longest = numWithLongestChain(1_000_000);

void main() {
    writefln("%d has the longest chain", longest);
}
June 05, 2011
simendsjo:

> It's fun to solve the euler problems at compile time, but I'm often running out of memory. Is there a way around this?

Don wants to improve the CT interpreter, so maybe your problem will eventually be solved.

Bye,
bearophile