/ / メモ
2007-03-03

コラッツ予想から
D言語で書いてみた
template版

template collatz(long n)
{
         static if(n <= 1)  const collatz = 0;
    else static if(n % 2)   const collatz = collatz!(n * 3 + 1) + 1;
    else const collatz = collatz!(n / 2) + 1;
}

関数版

long collatz(long n)
{
    if(n<=1) return 0;
    return (n % 2) ? collatz(n * 3 + 1)+1 : collatz(n / 2) + 1;
}

トラックバック http://mikanya.dip.jp/memo/2007-03-03-1