-3

出力と入力が同等になるまで、出力が入力として使用される python ループを作成しようとしています。これは、解決しようとしているステートメントです。

  1
----- = x 
 1+x

結果は黄金比 (0.618034...) よりも 1 少なくなります。私は紙の上でそれを行いましたが、小数点以下の数桁の精度を得るには約 20 ループが必要です。これを解決するために使用する Python ループのタイプを教えてください。

4

1 に答える 1

1

したがって、ここで説明していることに基づいて、指定された条件が true になるまで何かを実行し続ける必要があるため、while ループが必要です。

lastOutput = 0; # an arbitrary starting value: the last output value 
                # needs to be shared between loop cycles, so its 
                # scope must be outside the while loop

startingValue = # whatever you start at for input
finished = False # flag for tracking whether desired value has been reached
while (!finished):
    # body of loop:
    # here, you need to take lastOutput, run it through the 
    # function again, and check if the new output value is the
    # same as the input that created it. If so, you are done,
    # so set the flag to True, and note that the correct value is now stored in lastOutput
    # If not, set the new output as lastOutput, and go again!

# ...and now finish up with whatever you want to do now that you've 
# found the value (print it, etc.)!

値が同じかどうかをチェックするためのロジックに関しては、精度を高めるためにある種のしきい値が必要になります (そうしないと、永遠に実行されてしまいます!)。モジュール性のために独自のメソッドでそのチェックを記述することをお勧めします。日本酒。

これがお役に立てば幸いです。さらに実際のコードを投稿する必要がある場合はお知らせください (実際のコードをあまり公開しないようにしました)。

于 2013-06-14T18:04:52.807 に答える