エレファント・ビジュアライザー調査記録

ビジュアルプログラミングで数式の変形を表すことを考えていくブロクです。

たらい回し関数(6)

while の繰り返しが2回以下であることを考えると、以下のように書き直すことができます。これを見ると a2 と c2 は計算する必要がありません。

        public static string Test()
        {
            int count = 0;
            int depth = 0;
            float tarai(float x, float y, float z, int d)
            {
                count++;
                d++;
                depth = d > depth ? d : depth;
                if (x <= y)
                {
                    return y;
                }
                else
                {
                    float a = tarai(x - 1, y, z, d);
                    float b = tarai(y - 1, z, x, d);
                    float c = tarai(z - 1, x, y, d);
                    (x, y, z) = (a, b, c);
                    count++;
                    d++;
                    depth = d > depth ? d : depth;
                    if (x <= y)
                    {
                        return y;
                    }
                    else
                    {
                        float a2 = tarai(x - 1, y, z, d);
                        float b2 = tarai(y - 1, z, x, d);
                        float c2 = tarai(z - 1, x, y, d);
                        (x, y, z) = (a2, b2, c2);
                        count++;
                        d++;
                        depth = d > depth ? d : depth;
                        return y;
                    }
                }
            }
            return $"{tarai(12, 6, 0, 0)}, count = {count}, depth = {depth}";
        }