Swift Fun Algorithms #3: Factorials & Recursion

preview_player
Показать описание
Today's algorithm will be the relatively simple function known as the Factorial. I'm sure most of you guys know how a factorial works already. Thus lets go ahead and implement a simple for loop style factorial function. We will then move on to implementing the code using recursion! Trust me its a lot of fun.

Source code:

Рекомендации по теме
Комментарии
Автор

only 2000 view?? come on guys study your Swift Fun Algorithms!! This video is worth millions of views

nathanjeong
Автор

Really nice and informative series Brain specially for some one interested in diving into competitive programming using swift, please do more of these.

kryptoshi
Автор

I really love recursion. It can be a real brain tease but when you get it to work it's almost like watching magic happen. Really powerfull stuff.

danieledstrom
Автор

LOL@ his comment at the end. « Recursions take a little bit of thought »

cinquain
Автор

A more clear way to explain is
value * recursive(value - 1)
If value =3 then

3 * (3-1) * (2-1) * 1
If value = 4 then
4 * (4-1) * (3-1) * (2-1) * 1

story
Автор

Спасибо браток выручил, от меня подписка и лайк)))

МахмуджанНиязов
Автор

What should I do if I want to find the factorial of a large number like 25?

hasanalisiseci
Автор

ternary op will shorten it further ..

func factorial(of num : Int) -> Int! {
return num < 0 ? nil : num == 0 ? 1 : num * factorial(of: num - 1)
}

neeleshshah
Автор

I would try:
func factorial(of value: Int) -> Int {
(1...value).reduce(1, *)
}
Should work right? :)
I know it's not what this video is about, but it seems nicer

damiandudycz
Автор

error: Recursions.playground:15:46: error: missing argument label 'value:' in call
return value * - 1) This doesn't seem to be working :(

formulaint
Автор

Great! Thanks! Really love this series! Very very interesting.

andreychirkov
Автор

Hello Brian,
Excellent video again.
I have one suggestion/request: can you make the font size of your code a little bigger?
I am watching on a MacBook Pro 15 inch and it's kind of ok though a tiny bit small. But I find it very hard to watch on smaller screens like iPads because the font scales down with the dimension of the video.
Thanks again for the videos. I enjoy your informative and entertaining style.
Gilles

GillesVandenPeereboom
Автор

That is a damn good camera! what model is that?

ingongoyama
Автор

What software are you using to show the screen with?

kimberlynorton
Автор

Hey, serious question. What’s the benefit of using UInt here instead of Int? I’m sure you had a reason

david-tracy
Автор

Thanks for the video my man, I explained binary search to my gf today based on your video :)

krutomjer
Автор

So you are basically calling the function in its own definition, making it loop through itself as many times, as the argument number and each time multiplying by the product of the previous loop. Mind is blown, loving it!

TheAlderFalder
Автор

how about going through project euler problems and walk us through solutions, that would be awesome

karimgucci
Автор

Could any of you find the source code?

kimberlynorton