Comparing Iterative and Recursive Factorial Functions

Comparing Iterative and Recursive Factorial Functions

Khan Academy

13 лет назад

157,776 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Seng Keat
Seng Keat - 10.10.2022 10:02

can u recommend the books about the iteration

Ответить
Bara 79
Bara 79 - 01.10.2022 06:45

Missed your sound❤

Ответить
Candi Chiu
Candi Chiu - 10.07.2022 14:52

Thank you! very clear.

Ответить
Ujjval W
Ujjval W - 15.06.2022 01:29

Who is this Teacher?

Ответить
Daniel Ford
Daniel Ford - 10.10.2021 12:40

Chaos

Ответить
Sophia 028
Sophia 028 - 28.09.2021 04:38

Thank you sir!

Ответить
Nawfal Jafri
Nawfal Jafri - 12.09.2021 20:39

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

Ответить
Shreyash Kashyap
Shreyash Kashyap - 26.05.2021 11:12

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😉

Ответить
user_name_1188
user_name_1188 - 23.11.2020 21:09

Veeeryy cool job man, big thanks to you✌️

Ответить
mo liv
mo liv - 10.09.2020 19:52

not the comparison i expected ...

Ответить
Ritesh Saha
Ritesh Saha - 29.02.2020 23:45

which function is faster?

Ответить
Sarina Saman
Sarina Saman - 18.02.2020 19:06

recursion is a billion times better than iteration tbh

Ответить
Birobrata Deb
Birobrata Deb - 01.12.2019 17:27

Boom boom! Why is it so cutely explained!

Ответить
Instant Noodles
Instant Noodles - 12.07.2018 03:16

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

Ответить
(K12_HN) Bui Huy Hoang
(K12_HN) Bui Huy Hoang - 20.10.2017 20:13

Thanks god you're here.

Ответить
Serenity
Serenity - 13.10.2017 03:16

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

Ответить
Weilian Tan
Weilian Tan - 11.09.2017 16:39

What does return do ?

Ответить
lol k
lol k - 10.06.2017 15:35

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

Ответить
Jake Ambrose
Jake Ambrose - 19.04.2017 08:51

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

Ответить
Victor Nnah
Victor Nnah - 08.02.2017 01:32

Must I always return 1

Ответить
Petru Guta Ciucur
Petru Guta Ciucur - 03.01.2017 20:43

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

Ответить
Majesty
Majesty - 21.08.2016 22:18

thanks a lot

Ответить
Vin Lacey
Vin Lacey - 08.08.2016 23:38

Absolutely wonderful explanation of recursion. Well done!

Ответить
Nopal
Nopal - 05.07.2016 22:40

Thank you.

Ответить
DJBvon
DJBvon - 02.02.2016 06:42

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.

Ответить
DJ
DJ - 25.07.2015 14:30

Just had one of those "Oooooooh" moments. Thanks Khan!

Ответить
Dual Signal
Dual Signal - 19.11.2014 12:06

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

Ответить
Mars
Mars - 08.10.2012 06:32

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

Ответить
gexwing
gexwing - 30.05.2012 02:11

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.

Ответить
kerrywsmyth
kerrywsmyth - 03.02.2012 07:44

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.

Ответить
spirituelconnexion
spirituelconnexion - 10.11.2011 11:20

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

Ответить
Muhammad Choudhary
Muhammad Choudhary - 03.11.2011 06:06

YOU'RE AWESOME, THANKS!!!

Ответить
Alder
Alder - 05.07.2011 06:10

@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.

Ответить
myne4
myne4 - 30.06.2011 16:30

Recursion blows my mind.

Ответить
lundburgerr
lundburgerr - 30.06.2011 13:22

python is the shit!

Ответить
Riyadh Elalami
Riyadh Elalami - 30.06.2011 03:18

I love this guy

Ответить
DarkSamus100
DarkSamus100 - 30.06.2011 02:53

second. lol

Ответить