Python Forum
Can't understand the output of this recursion function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't understand the output of this recursion function
#1
def f(n):
    if n < 5:
        return n + 1
    
    return (f(n // 2) ,  f(n // 3) ) 
f(100) is leading to an output of
(((((4, 3), 5), (5, 3)), ((5, 3), (3, 2))), (((5, 3), (3, 2)), ((3, 2), 4)))




I broke down the recursions like this:


f(50), f(33)
f(25), f(11)
f(12), f(3)
f(6), 4 #recursion stops
f(3), 4
4, 4

So I expected an outcome of (4,4)


However, I have no idea why I'm getting an output of

((((4, 3), 5), (5, 3)), ((5, 3), (3, 2))), (((5, 3), (3, 2)), ((3, 2), 4)))


Thank you.
Reply
#2
Recursion stops on one branch... but your code spawns a new branch on almost every call to f()

                                    100
                    50                               33
            25               16              16              11
        12     8        8        5        8        5       5      3
      6    4  4   2    4   2    2   1    4   2   2    1   2   1   4
     3 2   5  5   3    5   3    3   2    5   3   3    2   3   2
     4 3
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Thank you! I finally understand. The recursion follows the left most branch and makes its way to the right.
Reply
#4
(Apr-04-2017, 09:39 PM)bigmit37 Wrote: Thank you! I finally understand.  The recursion follows the left most branch and makes its way to the right.

Not really... that's just a representation. The recursion happens in all the branches independently of what happens in the others.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
(Apr-04-2017, 10:40 PM)Ofnuts Wrote:
(Apr-04-2017, 09:39 PM)bigmit37 Wrote: Thank you! I finally understand.  The recursion follows the left most branch and makes its way to the right.

Not really... that's just a representation. The recursion happens in all the branches independently of what happens in the others.

Sorry, I meant the output. The output seems to be following the path from left to right.
Reply
#6
levels=[[] for _ in range(10)]

def f(n,l):
    if n < 5:
        levels[l].append(n+1)
        return n + 1
    levels[l].append(n//2)
    levels[l].append(n//3)    
    return (f(n // 2,l+1) ,  f(n // 3,l+1) )

print f(100,0)

for l in levels:
    if l:
        print ' '.join(map(str,l))
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a function akbarza 9 1,180 Sep-29-2023, 11:13 AM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,280 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  output correction using print() function afefDXCTN 3 11,075 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,208 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,110 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Don't Understand Recursive Function muzikman 9 3,668 Dec-03-2020, 05:10 PM
Last Post: muzikman
  Output with none, print(x) in function Vidar567 3 2,505 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  How to append to list a function output? rama27 5 6,721 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Lambda function recursion error DeadlySocks 1 2,050 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  Taking a CSV output and using it for another function Gduffley 1 1,569 Jan-30-2020, 12:13 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020