Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help explain this loop to me (python3)
New Posts  All Forums:Forum Nav:

Help explain this loop to me (python3)

post #1 of 3
Thread Starter 
so i have this loop that i need to understand, the furthest ive gotten is that this loop ends up running backwards because of how it is recursively called by v = f(n-1).
i understand the code is going to be run like this f(0),f(1)......f(6). what i don't understand is the actual values for n,r and v for each of these loops. i added print statements for each variable to see what was happening, it didn't not help that much.


Code:
def f(n):
        if (n==0):
                return 1
        else:
                v = f(n-1)
                r = n * v
                return r

n =6
print(n, f(n))

would someone mind explaining this to me?


and sorry if this is the wrong section,it was this or "school help" in off topic.
i7
(14 items)
 
  
CPUMotherboardGraphicsRAM
I7 3770k Sabertooth Z77 MSI 290X lightning 24gb ddr 1866 8-8-8-24 Crucial Ballistix elite 
Hard DriveHard DriveHard DriveOS
Intel 330 SSD WD black caviar 500gb Seagate barrcuda Windows 7 64bit 
MonitorKeyboardPowerCase
46inch Samsung lcd Razer lycosa Antec Truepower quattro 1000 NZXT Phantom (White) 
MouseAudio
Razer deathadder Samsung 5.1 surround sound. 
  hide details  
Reply
i7
(14 items)
 
  
CPUMotherboardGraphicsRAM
I7 3770k Sabertooth Z77 MSI 290X lightning 24gb ddr 1866 8-8-8-24 Crucial Ballistix elite 
Hard DriveHard DriveHard DriveOS
Intel 330 SSD WD black caviar 500gb Seagate barrcuda Windows 7 64bit 
MonitorKeyboardPowerCase
46inch Samsung lcd Razer lycosa Antec Truepower quattro 1000 NZXT Phantom (White) 
MouseAudio
Razer deathadder Samsung 5.1 surround sound. 
  hide details  
Reply
post #2 of 3
Quote:
Originally Posted by Zaid View Post

so i have this loop that i need to understand, the furthest ive gotten is that this loop ends up running backwards because of how it is recursively called by v = f(n-1).
i understand the code is going to be run like this f(0),f(1)......f(6). what i don't understand is the actual values for n,r and v for each of these loops. i added print statements for each variable to see what was happening, it didn't not help that much.
Code:
def f(n):
        if (n==0):
                return 1
        else:
                v = f(n-1)
                r = n * v
                return r

n =6
print(n, f(n))
would someone mind explaining this to me?
and sorry if this is the wrong section,it was this or "school help" in off topic.

This is an implementation of recursive factorial with a little less fluff so it's easier to understand.
Code:
def fact(n):
  if n == 0:
    return 1
  else:
    return n * fact(n-1)

print(fact(6))

This is a recursive program. It calls itself until it gets the final result of 1 at which point it starts returning the functions one by one.
Code:
#If we call
fact(6)

#since 6 != 0, we take the 'else' which returns n * fact(n-1)
6 * fact(6-1)
    5 * fact(4) #this is the answer to fact(5),
        4 * fact(3) #we must solve these before
            3 * fact(2) #we can return the function that called them
                2 * fact(1)
                    1 #return 1. Note, this doesn't call fact(), so our recursion ends
                2 * 1
            3 * 2
        4 * 6
    5 * 24
6 * 120
#so we get 720

Edited by hajile - 12/15/13 at 1:53am
post #3 of 3
Thread Starter 
Thanks for clearing that up.
i7
(14 items)
 
  
CPUMotherboardGraphicsRAM
I7 3770k Sabertooth Z77 MSI 290X lightning 24gb ddr 1866 8-8-8-24 Crucial Ballistix elite 
Hard DriveHard DriveHard DriveOS
Intel 330 SSD WD black caviar 500gb Seagate barrcuda Windows 7 64bit 
MonitorKeyboardPowerCase
46inch Samsung lcd Razer lycosa Antec Truepower quattro 1000 NZXT Phantom (White) 
MouseAudio
Razer deathadder Samsung 5.1 surround sound. 
  hide details  
Reply
i7
(14 items)
 
  
CPUMotherboardGraphicsRAM
I7 3770k Sabertooth Z77 MSI 290X lightning 24gb ddr 1866 8-8-8-24 Crucial Ballistix elite 
Hard DriveHard DriveHard DriveOS
Intel 330 SSD WD black caviar 500gb Seagate barrcuda Windows 7 64bit 
MonitorKeyboardPowerCase
46inch Samsung lcd Razer lycosa Antec Truepower quattro 1000 NZXT Phantom (White) 
MouseAudio
Razer deathadder Samsung 5.1 surround sound. 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help explain this loop to me (python3)