前の値を使用した計算

投稿者: Anonymous

下記のようなメソッドがあった場合example3, 6, 9をそれぞれ出力する際に、example6, example9を出力する時にもメソッドexample1から計算を始めるのですがこれだと時間がかかってしまいます。
1度example3を計算したらその結果をexample6, 9にそのまま当てはめることはできるのでしょうか。
私ではなかなか思いつきません。
何かありましたらアドバイスお願いします。

def example1a
    (6 + 5 + 9 + 10 + 4 + 6 +) / 6
end

def example1b
   a = example1a + 2/13 * (8 - example1a) 
   b = a + 2/13 * (9 - a)
   c = b + 2/13 * (7 - b)
   d = c + 2/13 * (3 - c)
   e = d + 2/13 * (8 - d)
   f = e + 2/13 * (5 - e)
   g = f + 2/13 * (4 - f)
   h = g + 2/13 * (10 - g)
   i = h + 2/13 * (9 - h)
   j = i + 2/13 * (6 - i)
   k = j + 2/13 * (7 - j)
   l = k + 2/13 * (8 - k)
end

def example2a
    (5 + 9 + 12 + 3 + 4 + 9 + 6 + 4 + 3 + 4) / 10
end

def example2b
    a = example2a + 2/27 * (5 - example2a) 
    b = a + 2/27 * (5 - a)
    c = b + 2/27 * (4 - b)
    d = c + 2/27 * (4 - c)
    e = d + 2/27 * (9 - d)
    f = e + 2/27 * (8 - e)
    g = f + 2/27 * (4 - f)
    h = g + 2/27 * (3 - g)
    i = h + 2/27 * (6 - h)
    j = i + 2/27 * (7 - i)
    k = j + 2/27 * (5 - j)
    l = k + 2/27 * (4 - k)
end

def example3
    example1b - example2b
end


def example4a
    (6 + 5 + 9 + 10 + 4 + 6) / 6
end

def example4b
   a = example4a + 2/13 * (8 - example4a) 
   b = a + 2/13 * (9 - a)
   c = b + 2/13 * (7 - b)
   d = c + 2/13 * (3 - c)
   e = d + 2/13 * (8 - d)
   f = e + 2/13 * (5 - e)
   g = f + 2/13 * (4 - f)
   h = g + 2/13 * (10 - g)
   i = h + 2/13 * (9 - h)
   j = i + 2/13 * (6 - i)
   k = j + 2/13 * (7 - j)
   l = k + 2/13 * (8 - k)
end

def example5a
    (5 + 9 + 12 + 3 + 4 + 9 + 6 + 4 + 3 + 4) / 10
end

def example5b
    a = example5a + 2/27 * (5 - example5a) 
    b = a + 2/27 * (5 - a)
    c = b + 2/27 * (4 - b)
    d = c + 2/27 * (4 - c)
    e = d + 2/27 * (9 - d)
    f = e + 2/27 * (8 - e)
    g = f + 2/27 * (4 - f)
    h = g + 2/27 * (3 - g)
    i = h + 2/27 * (6 - h)
    j = i + 2/27 * (7 - i)
    k = j + 2/27 * (5 - j)
    l = k + 2/27 * (4 - k)
end

def example6
    example4b - example5b
end


def example7a
    (6 + 5 + 9 + 10 + 4 + 6) / 6
end

def example7b
   a = example7a + 2/13 * (8 - example7a) 
   b = a + 2/13 * (9 - a)
   c = b + 2/13 * (7 - b)
   d = c + 2/13 * (3 - c)
   e = d + 2/13 * (8 - d)
   f = e + 2/13 * (5 - e)
   g = f + 2/13 * (4 - f)
   h = g + 2/13 * (10 - g)
   i = h + 2/13 * (9 - h)
   j = i + 2/13 * (6 - i)
   k = j + 2/13 * (7 - j)
   l = k + 2/13 * (8 - k)
end

def example8a
    (5 + 9 + 12 + 3 + 4 + 9 + 6 + 4 + 3 + 4) / 10
end

def example8b
    a = example8a + 2/27 * (5 - example8a) 
    b = a + 2/27 * (5 - a)
    c = b + 2/27 * (4 - b)
    d = c + 2/27 * (4 - c)
    e = d + 2/27 * (9 - d)
    f = e + 2/27 * (8 - e)
    g = f + 2/27 * (4 - f)
    h = g + 2/27 * (3 - g)
    i = h + 2/27 * (6 - h)
    j = i + 2/27 * (7 - i)
    k = j + 2/27 * (5 - j)
    l = k + 2/27 * (4 - k)
end

def example9
    example7b - example8b
end

puts exanmple3, exanmple6, exanmple9

解決

たとえば、

def example2a
    return @example2a_result if @example2a_result
    @example2a_result = (5 + 9 + 12 + 3 + 4 + 9 + 6 + 4 + 3 + 4) / 10
end

みたいな感じにすると、2回目以降の呼び出し時には計算せずに @example2a_result の値を返すだけになります。

回答者: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *