以下の内容はhttps://shinh.hatenablog.com/entry/20060713/1152735060より取得しました。


Collatz 予想を D _インタプリタ_ で

例のごとくコンパイルエラーで結果出力ですが、なるべくすなおーに書いてみました。 D のコンパイル時プログラミングはなかなか悪くないとわかるのではないでしょうか。

template g(int n) {
    const int result = n;
    static if (n == 1) {
        const int step = 1;
    }
    else static if (n & 1) {
        const int step = g!(3*n+1).step+1;
    }
    else {
        const int step = g!(n/2).step+1;
    }
}

template h(int m) {
    static if (m == 1) {
        const int step = 1;
        const int result = 1;
    }
    else {
        alias g!(m) head;
        alias h!(m-1) tail;
        static if (head.step > tail.step) {
            alias head max;
        }
        else {
            alias tail max;
        }
        const int step = max.step;
        const int result = max.result;
    }
}

static assert(false, h!(100).result);



以上の内容はhttps://shinh.hatenablog.com/entry/20060713/1152735060より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14