I'm working on building a python script to solve Riemann's Paradox. The idea is to take an inputted number, and add or subtract 1/2,1/3,1/4,1/5... to/from 1 until you have the inputted number. I'm running into the Recursion Error as a result of calling back to my comparison and adding/subtracting classes every time a comparison is made.
My code is as follows:
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
I'm wondering what I can do to solve this problem.