0

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.

CDspace
  • 2,639
  • 18
  • 30
  • 36
fjorn1
  • 5
  • 3
  • The program may never return from the first if statement in 'comparison()'. Instead of returning a value you are recursing back into the original function. Return values from the add/subtract functions. Do not recall the function that called it. – skytaker Nov 10 '16 at 16:24
  • @fjorn1 this is an interesting paradox, at first I was confused about how it would work, but now I think I've managed to fix the code :) – Tom Fuller Nov 10 '16 at 17:56

2 Answers2

0
  1. In current_number == goal you are comparing floats and using == is often not the right way to do it.

  2. You may want to increase the maximum recursion depth.

  3. Add print(current_number) as the first line of the comparison() function and see how your current_number converges to/diverges from the goal.

Community
  • 1
  • 1
SergiyKolesnikov
  • 7,369
  • 2
  • 26
  • 47
0

You don't need a function for add and subtract because adding a negative is the same as subtracting.

Also I have fixed your code, calling a function which the calls the function you called it from was what was causing your recursion error.

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
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
  • The sum 1 + 1/2 + 1/3 + ... is infinite. You are wrong to say that they get closer and closer to 2. – dmuir Nov 10 '16 at 17:12
  • Thank you! I'll spend some time trying to wrap my head around this... I'm just recently getting back into Python, and you would be amazed at how much you forget after a few years. – fjorn1 Nov 11 '16 at 23:09