www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ctfe - out of memory

reply simendsjo <simen.endsjo pandavre.com> writes:
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);
}
Jun 05 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Jun 05 2011