Comparing Iterative and Recursive Factorial Functions

preview_player
Показать описание
Comparing iterative and recursive factorial functions
Рекомендации по теме
Комментарии
Автор

Just had one of those moments. Thanks Khan!

DJ_Has_Thoughts
Автор

Thanks a ton Khan Academy. Idk why your videos don't get much views but your quality of teaching is remarkable! This is much better than the colleges/universities, that's why Elon Musk us with you guys😉

shreyashkashyap
Автор

it works this way: the function is still running while it calls the next function, so it builds up a stack of function calls (thus: call stack), then when it reached the base case, it returns a value, then the above returns, etc. etc. until the call stack has been reduced back to the original function call, which then returns the result, done. the variable number is NOT the same for all the function calls, each function call has its own variables.

gexwing
Автор

Absolutely wonderful explanation of recursion. Well done!

vinlacey
Автор

I love the concept of Programming.
But I really hate the method my college book is explaining this.
Thank you Khan Academy!!!

RUCERAl
Автор

the way you write it and show it here was enlightening for me

jakeambrose
Автор

so basically (i think, idk cuz I'm just watching this video for fun, i dunno much about coding so correct me if I'm wrong)
for 0, we specify that if product = 0 or less, then return product = 1 since a 0 in this formula would mean 0(0+1) which is 0x1=0; what's happening is basically we take an integer (integers start from 0 unlike counting numbers that start from 1) and we put it in the formula 1(i+1) where i is the integer and the 1 outside of the parenthesis is the previous product/factorial since the product of 0 factorial is 1, we start with 1; as it iterates, the previous factorial product becomes the new product outside of the parenthesis we're trying to find so we continue doing this for the number of times we want a factorial for so 3! would be 6 because 1(0+1) = 1, 1(1+1) = 2, 2(2+1) = 6 so the to calculate the factorial we could also do:

def factorial(number):
if number <= 1:
return 1
else:
product = 1
for i in range(number):
product = product * (i+1)
return product

nawfaljafri
Автор

This video doesn't provide any comparison like the title says. It's more of a walkthrough of an iterative and recursive example.

DualSignal
Автор

Boom boom! Why is it so cutely explained!

bir_deb
Автор

Sal, watching this playlist makes me want to switch my major to CS :(:(:(

spirituelconnexion
Автор

Very well explanation. So the program stacks the numbers n in memory then go back and solve each multiplication.

petruciucur
Автор

Veeeryy cool job man, big thanks to you✌️

SB-igrg
Автор

note, that i + i should be i + 1? yes?

serenity
Автор

I would never guess recursion would ever work because it APPEARS as if you are redefining the value of the variable "number" and returning a result every time the function recurs. The thing that needs explaining for me is how Python knows to remember each of the returned results for each recurring function call.

kerrywsmyth
Автор

I have to admit, almost went to another video after hearing a mistake in the "iterative" pronunciation within the very first verbal audible of this video.

djbvon
Автор

can u recommend the books about the iteration

tubex
Автор

@radiator0505 I think it has to do with the definition of a factorial. It works fine when the numbers are decreasing, but the definition of a factorial given a negative number is different (since the numbers are increasing each time rather than decreasing).. this results in a divide-by-zero error when you get to 1. Why the program returns 1 I am not sure.

AlderDragon
Автор

hey tech me if we put njmber=1 in if statement so ans is 1 how

lolk
Автор

recursion is a billion times better than iteration tbh

SarinaSaman
Автор

you can't explain recursions with functions in 2 minutes...

MarsTheProgrammer
welcome to shbcf.ru