lim[n→∞]√2↑↑n=2

id:athos:20081028:p1
g:csnagoya-sicp:id:Gemma:20081029
g:csnagoya-sicp:id:Gemma:20081030

30分プログラム、その403。なんとなくボクも参加しないといけない気がしたので、書いてみる。
げんまさんが無限ストリームで計算してたので、ボクはPythonイテレータで挑戦してみる。

使い方

$ python tower.py | cat -n
     1	1.41421356237
     2	1.63252691944
     3	1.76083955588
....
    68	1.99999999999
    69	1.99999999999
    70	2.0
    71	2.0

あれ、2に収束しちゃった。まあ、誤差の問題でしょう、たぶん。

ソースコード

#! /usr/bin/python
# -*- mode:python; coding:utf-8 -*-
#
# tower.py -
#
# Copyright(C) 2008 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2008/10/30 22:26:04
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#

import math

def tower_stream(n):    
    x = n
    while True:
        yield x
        x = n ** x

for n in tower_stream(math.sqrt(2)):
    print n