0

I got some error after a limited time. When i tried it to javascript but there also the same error i don't know what is the problem
if you set the z = 50 and target = 33 the error you will find
I want that fix this error. So i can calculate limit less number.

x = 3
target = 81

a = -1*x
b = -1*x
c = -1*x

def main():

    def result():
        global a
        global b
        global c

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        print("Your result:")
        print(a)
        print(b)
        print(c)
        print(cheack)

    def math_1():
        global a
        global b
        global c

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            result()
        elif a == x:
            math_2()
        else :
            a += 1
            math_1()

    def math_2():
        global a
        global b
        global c

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            result()
        elif b == x:
            math_3()
        elif a == x:
            a = -1*x
            b += 1
            math_1()
        else:
            print("Error!")

    def math_3():
        global a
        global b
        global c

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            result()
        elif c == x:
            print("Sorry.. Arman!")
        elif b == x:
            a = -1*x
            b = -1*x
            c += 1
            math_1()
    math_1()
main()

I got some error after a limited time. When i tried it to javascript but there also the same error i don't know what is the problem if you set the z = 50 and target = 33 the error you will find I want that fix this error. So i can calculate limit less number.

  • Can you explain what you are trying to do? –  Jul 10 '21 at 12:05
  • I want that if i put any int number like 55 then it will give a value of three number a , b, c which will be able to support. 55 = ((a*a*a)+(b*b*b)+(c*c*c)) – Arman Hossen Jul 10 '21 at 12:08
  • 1
    Python can’t recurse infinitely, or anywhere near infinitely. Read this to find out how to get the recursion limit for your Python in your environment https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it#3323013 – DisappointedByUnaccountableMod Jul 10 '21 at 12:08
  • I want that if i put any int number like 55 then it will give a value of three number a , b, c which will be able to support. 55 = ((a * a * a)+(b * b * b)+(c * c * c)) – Arman Hossen Jul 10 '21 at 12:14
  • you only want integers? – Christian Sloper Jul 10 '21 at 12:16

2 Answers2

1

Python has limited callstack size, that is, how deep you can go while calling function in function in function and so on before you get to the limit.

Since your recursion is linear and not branching, you don't need a stack, only a state variable to convert this recursion into a loop.

x = 3
target = 81

a = -1*x
b = -1*x
c = -1*x
state = 1

def main():

    def result():
        global a
        global b
        global c
        global state

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        print("Your result:")
        print(a)
        print(b)
        print(c)
        print(cheack)
        state = -1

    def math_1():
        global a
        global b
        global c
        global state

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            state = 0
        elif a == x:
            state = 2
        else :
            a += 1
            #state is already 1

    def math_2():
        global a
        global b
        global c
        global state

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            state = 0
        elif b == x:
            state = 3
        elif a == x:
            a = -1*x
            b += 1
            state = 3
        else:
            state = -1
            print("Error!")

    def math_3():
        global a
        global b
        global c
        global state

        cheack = ((a*a*a)+(b*b*b)+(c*c*c))

        if cheack == target:
            state =0
        elif c == x:
            state = -1
            print("Sorry.. Arman!")
        elif b == x:
            a = -1*x
            b = -1*x
            c += 1
            state = 1
    
  while state >= 0:
    if state == 0:
      result()
    elif state == 1:
      math_1()
    elif state == 2:
      math_2()
    elif state == 3:
      math_3()

main()

This, of course, can be written much more cleanly. That is, however, homework for the reader.

Kryštof Vosyka
  • 566
  • 3
  • 15
0

Last night i lost hope. But in the morning i got some crazy idea and it worked. It worked more then my expectation.

target = int(input("Give your target: "))
x = int(input("Give a limit: "))

a = -1*x
b = -1*x
c = -1*x
cheack = (a*a*a+b*b*b+c*c*c)

sum_Max = (((x*2)+1)**3)
print("Max SUM: ", sum_Max)

while cheack != target:
    cheack = (a*a*a+b*b*b+c*c*c)
    if cheack == target:
        print("Available: ")
        print(" a =", a)
        print(" b =", b)
        print(" c =", c)
    elif a == x:        
        if b == x:
            if c == x:
                print("Not available... Try in bigger numbers")
                break
            else :
                a = -1*x
                b = -1*x
                c += 1
        else :
            a = -1*x
            b += 1
    else :
        a += 1
  • But there is a problem it take so long when i put number more than 999. How can i use multi threading on this python script to use my 4 core and 8 threats to calculate faster – Arman Hossen Jul 11 '21 at 08:51