2016-11-10 14 views
0

私はRiemannのParadoxを解決するためにPythonスクリプトを作成しています。入力された数字を取り、1/2,1/3,1/4,1/5 ...を1/2に加算または減算することです。比較を呼び出すと、比較が行われるたびにクラスを追加/減算した結果、再帰エラーが発生します。次のようにRecursionError:最大再帰の深さを超えました

私のコードは次のとおりです。

import math 
#declare values of variables before they are referenced 
goal = float(input("What number are you trying to achieve?")) 
current_number = float(1) 
negatives = (2) 
positives = (3) 
#----------------------------------------------------- 
def add(goal,current_number,positives,negatives): #define the add operation for when the current number is less than the goal 
    current_number = current_number+(1/positives) #add current fraction to the current number 
    positives = positives+2 #Set the next additional fraction 
    comparison(goal,current_number,positives,negatives) #go to comparision of the current number to the goal 

def subtract(goal,current_number,positives,negatives): #define the subtract operation for when the current number is greater than the goal 
    current_number = current_number-(1/negatives) #subtract current fraction from the current number 
    negatives = negatives+2 #set the next subtractional fraction 
    comparison(goal,current_number,positives,negatives) #go to comparision of the current number to the goal 

def comparison(goal,current_number,positives,negatives): #define comparison between the current number to the goal 
    if current_number < goal: 
     add(goal,current_number,positives,negatives) #if the current number is less than the goal, go to add the next fraction to it 
    if current_number > goal: 
     subtract(goal,current_number,positives,negatives) #if the current number is greater than the goal, do to subtract the next fraction from it 
    if current_number == goal: 
     print("The equation for your number is 1+1/3...") 
     print("+1/",positives) 
     print("...-1/2...") 
     print("-1/",negatives) 
     #if the current number is equal to the goal, print the results 

comparison(goal,current_number,positives,negatives) #do the comparison between the current number and the goal 

私はこの問題を解決するために何ができるかと思いまして。

+0

「比較()」で文の場合は、プログラムが最初から返さないことがあります。値を返す代わりに、元の関数に再帰しています。加算/減算関数からの値を返します。呼び出された関数を呼び出さないでください。 – bejota

+0

@ fjorn1これは興味深いパラドックスです。最初はどのように動作するのか混乱しましたが、今はコードを修正することができたと思います:) –

答えて

0

負の値を加算することは減算と同じであるため、加算と減算の関数は必要ありません。

また、呼び出された関数を呼び出す関数を呼び出して、再帰エラーを引き起こしていたコードを修正しました。

Hope this helps you :)

import math 
goal = float(input("What number are you trying to achieve?")) 

current_number = 0 
fraction = 2 
#----------------------------------------------------- 
def change(goal, current_number, fraction, sign = 1): #define the add operation for when the current number is less than the goal 
    global positives 
    current_number = current_number + (1/fraction) * sign # add or take away current fraction to the current number 
    return current_number 

def comparison(goal, current_number, fraction): #define comparison between the current number to the goal 
    print("The equation for your number is:") 
    print("1/2") 
    while round(current_number, 3) != round(goal, 3): # you don't have to round if you don't want to 
     fraction = fraction + 1 
     if current_number < goal: 
      print("+1/"+str(fraction)) # positive 
      current_number = change(goal, current_number, fraction) #if the current number is less than the goal, go to add the next fraction to it 
     elif current_number > goal: 
      print("-1/"+str(fraction)) # positive 
      current_number = change(goal, current_number, fraction, -1) 
    print("...") 

comparison(goal,current_number, fraction) #do the comparison between the current number and the goal 
+0

合計1 + 1/2 + 1/3 + ...は無限大です。あなたは2に近づくと言って間違っています。 – dmuir

+0

ああ、私の間違い。私は私の答えを編集します。 –

+0

ありがとう!私はこれを念頭に置いて時間を割いています...私はちょうど最近、Pythonに戻っています。あなたは数年後にどれくらい忘れてしまったのか驚いています。 – fjorn1

0
  1. current_number == goalでは、浮動小数点数を比較すると、多くの場合、the right way to do itない==を使用しています。

  2. increase the maximum recursion depthです。

  3. comparison()関数の最初の行に「print(current_number)」を追加し、current_numberがゴールに収束/分岐する様子を参照してください。

+0

...リストを使用してコードを書き直したい場合があります再帰的ではなく反復的です。 – spectras

関連する問題