単利・複利計算

30分プログラム、その733。またもmixiからです。
問題文がよく分からなかったので、単利の計算と複利の計算をする関数を書け、と理解しました。たぶん違うけど。

複利計算を

def complex(interest, x):
  chain([x],complex(interest, x * (1 + interest)))

でやろうとしたら、complexの呼び出しが止まりませんでした。
complexの呼び出しを遅延したいんだけど、どうしたらいいんだろう。前も同じようなことで悩んで、結局あきらめた気がする。

使い方

print take(10,simple(0.1,100))
# => [100.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0]

print take(10,complex(0.1,100))
# => [100, 110.00000000000001, 121.00000000000003, 133.10000000000005, 146.41000000000008, 161.0510000000001, 177.15610000000012, 194.87171000000015, 214.3588810000002, 235.79476910000022]

ソースコード

#! /usr/bin/python
# -*- mode:python; coding:utf-8 -*-
#
# simle-complex.py -
#
# Copyright(C) 2010 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2010/02/08 21:44:39
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#
from itertools import *

def simple(interest, x):
    delta = interest * x
    return imap(lambda n : x + n * delta, count(0))

def complex(interest, x):
    # chain([x],complex(interest, x * (1 + interest)))
    yield x
    for y in complex(interest, x * (1 + interest)):
        yield y

def take(n, seq):
    return list(islice(seq, n))

print take(10,simple(0.1,100))
print take(10,complex(0.1,100))