Recursion - CS50 Shorts

  Рет қаралды 171,169

CS50

CS50

Күн бұрын

Пікірлер: 248
@shruti8576
@shruti8576 4 жыл бұрын
he just called a few lines of code sexy.... that's when u know ur talkin to about a real programmer.
@derrylmartinez8010
@derrylmartinez8010 4 жыл бұрын
lolllll
@ktappdev
@ktappdev 2 ай бұрын
It is tho
@1d_4e32
@1d_4e32 Жыл бұрын
you calling those lines of code sexy just made my day
@topincreible7856
@topincreible7856 4 жыл бұрын
Recursion: *exists* Me: uhhh..that's sort of cool Doug: SEXAYYY!!!
@tentotheace
@tentotheace 6 жыл бұрын
7:33 True programmer love. :D You're the best, Doug.
@londonstudioliving
@londonstudioliving 4 жыл бұрын
When he described recursion as sexy I almost spat out my breakfast LOOOL.
@AidilGoh
@AidilGoh 4 жыл бұрын
sexayyy 😅
@gabrielnastari8513
@gabrielnastari8513 5 жыл бұрын
You are transforming the life of a class of 15 people in Brazil, doug! Thank you so much Malan and Doug!
@marcusxavierr6616
@marcusxavierr6616 4 жыл бұрын
Parabéns amigo
@joaovitorpereira9874
@joaovitorpereira9874 4 жыл бұрын
Parabéns amigo
@TheOMGbeandip
@TheOMGbeandip 4 жыл бұрын
shoutout my boy Bryan one time
@upsidedownChad
@upsidedownChad 2 жыл бұрын
Encontrei um brasileiro🇧🇷
@superdude512
@superdude512 4 жыл бұрын
7:33 I pity the fool who only watches the main lecture (even though those are great) because they'll miss gems like this. And Doug's a damn good teacher.
@BatuhanÇelik-d6f
@BatuhanÇelik-d6f 9 ай бұрын
so you're telling me there are people who can solve psets with only main lecture?
@maelvgx9363
@maelvgx9363 5 жыл бұрын
Paused the video cause I didnt want to disapoint doug !
@Paulxl
@Paulxl 3 жыл бұрын
I paused the video and tried to do by myself, but I don't get the final example.
@johnhansen3964
@johnhansen3964 4 жыл бұрын
Find you a man who talks about you the way Doug talks about recursion
@andreasv9472
@andreasv9472 2 жыл бұрын
underrated
@tigana
@tigana 6 ай бұрын
Literally
@jannegrey
@jannegrey 6 жыл бұрын
I took CS50X couple of years ago. I didn't finish, but it taught me more about programming than anything else.
@jayant9151
@jayant9151 6 жыл бұрын
Jan Negrey why don't u complete then?
@KillTheAlarm69
@KillTheAlarm69 4 жыл бұрын
@@jayant9151 that's what I asked myself recursively
@sanuraj5998
@sanuraj5998 3 жыл бұрын
@@KillTheAlarm69 what's your base condition?
@laurenbellamy9611
@laurenbellamy9611 4 жыл бұрын
"You may recall from elementary school days" lol Doug no one learned about Fibonacci in elementary
@kattodoggo3868
@kattodoggo3868 4 жыл бұрын
Doug did :D
@nayankumarbarwa4317
@nayankumarbarwa4317 2 жыл бұрын
@@kattodoggo3868 doug was born in harvard
@anglaismoyen
@anglaismoyen 2 ай бұрын
We did in the UK.
@fahimhuq2768
@fahimhuq2768 4 жыл бұрын
I Love the little finger tap he does on 7:33 LoL
@ojalafsenior4757
@ojalafsenior4757 Жыл бұрын
This video just made recursive algorithms very sound interesting and easy to follow
@martinlancaster5109
@martinlancaster5109 2 жыл бұрын
Watching this, i find myself constantly saying, "This is amazing". Thanks Doug.
@gona3665
@gona3665 3 жыл бұрын
This guy's been coding for so long that it now turns him on
@anshsatwani3130
@anshsatwani3130 4 жыл бұрын
2:15 "couple of facts, pun intended" damn Dough, you're funny too.
@twizzy8783
@twizzy8783 Жыл бұрын
bro said the code is sexy
@Kylebacca
@Kylebacca Жыл бұрын
If anyone else was confused during the fib sequence part when he says if n == 0 return 1, elsif n == 2 return 1 Base cases should be if n == 0 return 0, elsif n == 1 return 1, else fib(n-1) + fib(n-2)
@francescorossato9958
@francescorossato9958 Ай бұрын
If you are from TOP Ruby's course here is my solution for the Collatz conjecture done in Ruby: --spoiler-- def collatz_recursive(n,steps=0) return steps if n == 1 if ((n % 2) == 0) return collatz_recursive((n / 2), steps += 1) else return collatz_recursive((n * 3) + 1, steps += 1) end end collatz_recursive(8) #=> returns 3 for example
@gbrl10
@gbrl10 Жыл бұрын
Collatz function could be written this way too: function collatz(n, total = 0) { if (n == 1) { return total; } else if (n % 2 == 0) { return collatz(n / 2, (total += 1)); } else { return collatz(3 * n + 1, (total += 1)); } } But I think the one in the video I think is more elegant.
@existentialcrysis9850
@existentialcrysis9850 Ай бұрын
this was also my solution
@izaccy
@izaccy 5 жыл бұрын
Jesus this was high quality and explained super well with easy concepts, I went through a bunch of tutorials that used complex terms etc... looking at u hackerrank... but thanks CS50 and Doug
@issamramdani825
@issamramdani825 8 ай бұрын
I didn't expect it to be that easy. Thanks for the explanation, really. Good luck, guys, and don't give up.
@droptopboatmobile
@droptopboatmobile Жыл бұрын
7:05 everything in lecture that confused me clicked. Thanks Doug~!
@rod6722
@rod6722 26 күн бұрын
Best explanation of recursion on the Internet.
@denys.panchenko
@denys.panchenko 3 жыл бұрын
Opened it here from CS50 official website just to praise the passion of the tutor! Indeed, programming is sexy!
@juansalomon1609
@juansalomon1609 Жыл бұрын
Amazing explanation to such a abstract topic!. I shortened it a little more function factorial(n){ return n==1 ? 1 : n* factorial(n-1); } console.log(factorial(5)); //120
@javierdemendonca257
@javierdemendonca257 10 ай бұрын
This was MIND BLOWING. Happy I got to make the function work, but needed DDB to point me to the solution of "return + 1", would have never EVER figured that one out, although once explained makes much sense. Thank you for a terrific short, very helpful :)
@anaveronicaaponte
@anaveronicaaponte 4 жыл бұрын
No one: Doug: So here there's a couple of facts... Pun intended Me: HAHAHAHA
@annette-diana_majchrowicz
@annette-diana_majchrowicz 4 жыл бұрын
The "Call Stacks" short includes an extremely delighting visualization on this, see 3:20 in kzbin.info/www/bejne/l3SznKawmrCFl5Y
@stonehead66
@stonehead66 3 ай бұрын
Thanks! This helped a lot!
@johnhansen3964
@johnhansen3964 4 жыл бұрын
Way to make me feel dumb for not learning the Fibonacci sequence in Elementary school and waiting until I was a dumb teenager getting into Tool to learn about it
@twopaypal_f1
@twopaypal_f1 Жыл бұрын
for mine, i initialized int i = 0 as a global variable. my collatz function had three if statement for the three different possibilities. then created a counter in my collatz function that would i++ every time the collatz function called itself. then returned total i after n == 1 and printed it.
@asfnksenpai3508
@asfnksenpai3508 Жыл бұрын
I did something like this too but because i was initializing the counter variable at the top it was always returning same value 😂 then i tried declaring it as static
@EL_WAFI_CHERKAOUI
@EL_WAFI_CHERKAOUI Ай бұрын
i solved the last exercise correctly but in slightly different way : function Colatz(number, steps=0){ if(number
@learning8160
@learning8160 Ай бұрын
7:32 Real Programmer
@meepable
@meepable 6 жыл бұрын
About the collatz conjucture, the recursive cases have the return + 1. I tested with the + 1 and without the tests, there are same results. I'm curious to see your point of view on putting the +1 in those recursive cases. Before checking your answer, I put the return -1 in the end of the code to avoid the return error and -1 will disprove the conjecture (which is impossible). And, thank you for making this video!
@ares106
@ares106 2 жыл бұрын
That return+1 is confusing the hell out of me, when I wrote my version of this function I used a global variable “steps” as a counter that I would ++ increment right before the return statement if the number is even or odd. I didn’t dare mess with the return value. Wish he spent a little time explaining exactly how that return works.
@robm7887
@robm7887 2 жыл бұрын
@@ares106 yeah how is this working without a step counter? I also do not understand even after debugging and watching it go step by step, where is it storing the value?
@SebasRyz
@SebasRyz 2 жыл бұрын
@@robm7887 I used a step counter too, but i imagine that when he return 1 + the fuction itself, that + 1 is waiting to be returned to the Main function calling it, but since the recursive function is calling itself, the +1 keeps waiting... Eventually we enter in the conditional of even or odd again and we return +1 again with the recursive function, so now that +1 that was waiting transforms into a +2, so on and so on, until n == 1, wich returns 0 (+2 + 0 for example). Thats when the function is called to MAIN and return the actual amount of steps without a step counter variable... or thats my understanding of it at least haha.
@iSgapetti
@iSgapetti Жыл бұрын
@@ares106 When I initially wrote the program, I also used a global variable, but with Doug's explanation, I modified it to this: I used a variable inside main to invoke the collatz function: int steps = collatz(n); The return value gets stored in the variable "steps" and gets a sum with each recursion or something.
@Surefire0
@Surefire0 10 ай бұрын
@@ares106 Same here. Luckily, I or we have access now to great mentor ChatGPT!!. Learning nowadays is much easier. ChatGPT: I see the confusion. In the context of a recursive function, the return 1 statement doesn't mean the end of the entire program or code; instead, it means the end of the current invocation of the function. When a function is called recursively, each invocation of the function has its own set of local variables and executes its own set of statements. The return statement is used to return a value to the caller of that specific invocation. In the Collatz sequence example: return 1 + collatz(n * 3 + 1); This line means that the current step in the Collatz sequence is being counted (represented by 1 +) before making the recursive call to collatz with the updated value. The result of this expression is then returned to the previous invocation in the recursion stack, and the steps are accumulated as each recursive call completes. So, in a recursive function, return 1 is a way to contribute a value to the final result while also continuing the recursive process. It doesn't end the entire program or code; it's
@ra4559
@ra4559 3 жыл бұрын
I get this. But I find recursion very hard to implement in my code. I always just want to iterate.
@lucashoww
@lucashoww Жыл бұрын
Doug Lloyd, the mind-blower!
@KyleAndersonMusic
@KyleAndersonMusic 6 жыл бұрын
Thank you so much for posting this Vid! I'm currently taking Data Structures at another University and this video made the concept easier to understand than my professor's lecture!
@vladimirs.1615
@vladimirs.1615 4 жыл бұрын
He's Doug Lloyd! This is CS50!)) Thanks Doug!
@tommasoorlandi4095
@tommasoorlandi4095 4 жыл бұрын
Can anyone explain me,in a very "coding for dummies way", why in the collatz recursion the return value is RETURN 1 + collatz?
@davidgodovich597
@davidgodovich597 4 жыл бұрын
The end goal is to return the number of steps needed to get to 1. Saying return 1 + collatz is incrementing 1 for every time the function is called.
@exnihilonihilfit6316
@exnihilonihilfit6316 4 жыл бұрын
@@davidgodovich597 Watch the video on call stacks: kzbin.info/www/bejne/l3SznKawmrCFl5Y
@bskonik
@bskonik 2 жыл бұрын
@@davidgodovich597 This is so helpful. Thank you!
@bskonik
@bskonik 2 жыл бұрын
@@exnihilonihilfit6316 This was a perfect connector. Thank you!
@WicCaesar
@WicCaesar 2 жыл бұрын
Because if n == 1, it doesn't count a step. But if it's higher than 1, it should count a step for each recursion. In my code, I simply incremented steps++, then called the recursion, and it worked. However, I had to declare int steps in the main function and pass it along with the user input.
@marketsareopenmao
@marketsareopenmao 4 жыл бұрын
Full code Factorial problem #include #include int factorialFunction(int n); int main(void) { //to creation a function that calls itself int n = get_int("choose a number buddy: " ); int productSum = factorialFunction(n); printf("yes this worked! %i", productSum); } int factorialFunction(int n) { int sum = 0; // base case if (n == 1) { return 1; } else { sum = n * factorialFunction(n - 1); } return sum; }
@gurleensingh2600
@gurleensingh2600 2 жыл бұрын
why did you set int sum = 0?
@ruslan9051
@ruslan9051 Жыл бұрын
You can also use this #include #include int factorial(int number); int main (void) { int number = get_int("Enter a number: "); printf("Factorial of your number is: %i ", factorial(number)); } int factorial(int number) { if (number == 1) return 1; else return number * factorial(number - 1); }
@Hex-Scholar
@Hex-Scholar Жыл бұрын
My solution in Python :) def collatz_conjecture(n, step=0): if n == 1: return step else: # n % 2 return 0 for even, not 0 = 1 (Truthy Value) if not n % 2: return collatz_conjecture(n/2, step+1) if n % 2: return collatz_conjecture(3*n+1, step+1) print("Steps: ", collatz_conjecture(7))
@lorenzobandinelli2638
@lorenzobandinelli2638 4 жыл бұрын
7:33 THIS IS CS50!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@readytocode4425
@readytocode4425 4 жыл бұрын
CS50 is an overall sexy course!
@geek_programmer8379
@geek_programmer8379 4 жыл бұрын
can anyone tell why the heck he adds 1 to collatz(n/2) or collatz(3n+1) ? thanks in advance :)
@hussainmusadaq796
@hussainmusadaq796 4 жыл бұрын
+1
@kremen1571
@kremen1571 4 жыл бұрын
It's a counter. Try it with debug50 with +1 and without. And you'll clearly see it step by step.
@mohammedalbusaidi4129
@mohammedalbusaidi4129 4 жыл бұрын
so at the end gives you the the total steps required to reach 1
@osmirog1936
@osmirog1936 4 жыл бұрын
I also don't understand why he adds 1 to the function return value -- how are steps counted in this way? I used a counter in order to count steps.
@osmirog1936
@osmirog1936 4 жыл бұрын
(Copy-pasting my comment below) I got it! After completing the Collatz's algorithm, the collatz function in the base case will eventually return 0, so we can use its return value as a counter at this point. After that, the next function refers to the function that returned 0 and adds 1 to it. The next function takes the value of the function that returned 1 and adds 1 to it, and so on in recursion. The number returned by the last function will be the total number of functions involved in the recursion.
@celicaman2zzfe
@celicaman2zzfe Жыл бұрын
Now it all make sense I've raking my brain on this. Thank you!!
@mars9842
@mars9842 Жыл бұрын
4 lines of code looking extra dapper lol. I LOLED at kind of sexy. Great explaination!
@kevinmutwirimwenda
@kevinmutwirimwenda Жыл бұрын
This was a very good explanation. Thank you.
@Liam-c5k
@Liam-c5k 6 ай бұрын
I did the Collatz one like this instead, for me it was easier to understand: function collatz(number, steps = 0){ if(number === 1){ return steps; } if(number % 2 === 0){ return collatz(number/2, steps + 1); } else{ return collatz(3*number + 1, steps + 1); } }
@LagrangePoint0
@LagrangePoint0 2 ай бұрын
That's exactly the way I solved it before seeing his answer.
@snafu4714
@snafu4714 4 жыл бұрын
If the collatz function eventually returns 0 to mean true/done with an iteration and we add +1 to act as a counter, why isn't that just built into the function? What use case would returning 0 have?
@HUNrobar
@HUNrobar 3 жыл бұрын
at some point you need to return something or it will be an infinite "loop". 0 means n was already 1 to begin with so that doesn't count as a step in this case.
@jesseliverless9811
@jesseliverless9811 2 жыл бұрын
Hello The Odin Project students :)
@samuelboyle8627
@samuelboyle8627 4 жыл бұрын
Just to help people out since this took me a while since he is assuming you know he is calling a function collatz. Basically I am just showing you need to have an int main(void) that calls the Collatz. This video also helps which is from week 4 kzbin.info/www/bejne/l3SznKawmrCFl5Y #include #include int collatz(int n); int main(void) { printf("%i ", collatz(15)); } int collatz(int n) { // base case if (n == 1) { return 0; } else if ((n % 2) == 0) { return 1 + collatz(n/2); } else { return 1 + collatz(3*n + 1); } }
@Gonbatfire
@Gonbatfire 9 ай бұрын
"Yes this is indeed sexy" (me looking at my Collatz function)
@unkoterebi
@unkoterebi 3 ай бұрын
Hello from TheOdinProject Student👋
@EL_WAFI_CHERKAOUI
@EL_WAFI_CHERKAOUI Ай бұрын
hi bro keep going
@lhandat
@lhandat 6 жыл бұрын
This is incredibly helpful. Thank you!
@adgardos
@adgardos 4 жыл бұрын
Doug sos un campeon!
@patrickmcauliffe7162
@patrickmcauliffe7162 4 жыл бұрын
I love you Doug!
@michaelrandazzo3959
@michaelrandazzo3959 Жыл бұрын
It clicked right at the end. I didn't call the function each time, I just ran either n/2 and didn't return anything.
@NaughtyGooseGaming
@NaughtyGooseGaming 4 жыл бұрын
My code looks more redundant than this. but where in this code is it returning the number of steps it took to get to 1? and if i want to print out that number?
@jonnysegway7866
@jonnysegway7866 Ай бұрын
I have just been trying to work collatz out i.e. where is the counter!! He doesn't explain what is going on here. but you notice he returns a 1 on each call, yeah the program makes a recursive call but only actually returns 1. So each time a call is made 1 is returned. So say when, in your main() you have "printf("Steps %i ",collatz(6));" collatz(6) is replaced with 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1.
@ArunaKhudan
@ArunaKhudan 4 жыл бұрын
i cornfused, have to watch this again
@user-cz1ex7kf2g
@user-cz1ex7kf2g 3 жыл бұрын
Another way to write the body of fact(): n == 1 ? 1 : n * fact(n - 1)
@tsvetanayvanova3145
@tsvetanayvanova3145 4 жыл бұрын
A coding poet!...💖
@adnanzaid6086
@adnanzaid6086 6 жыл бұрын
Thank you, this really helped. It is greatly appreciated!
@viky2002
@viky2002 2 жыл бұрын
Tideman smiling at you
@marketsareopenmao
@marketsareopenmao 4 жыл бұрын
Full code Collatz problem #include #include int collatzConjecture(int n); int main(void) { //to creation a function that calls itself int n = get_int("choose a number buddy: " ); int collatzReturn = collatzConjecture(n); printf("the collatz return was %i ", collatzReturn); } int collatzConjecture(int n) { int turns; if (n == 1) { return 0; } else if(n % 2 == 0) { turns = 1 + collatzConjecture(n/2); } else { turns = 1 + collatzConjecture(3*n + 1); } return turns; }
@yauya
@yauya 2 жыл бұрын
thank you so much you just help me with a question
@peiyunlau
@peiyunlau 4 жыл бұрын
Me too. I don't quite get why we have to return 1 with the Collatz conjecture.
@iEatBass
@iEatBass 4 жыл бұрын
its counting backwards. Lets say you do collatz(4). Is 4 == 1? no is 4 % 2 == 0? Yes (even) return 1 (as in one step) + collatz(4/2) Now what is collatz(4/2)? 4/2 = 2 so: is 2 == 1? no is 2 % 2 == 0? yes return 1 + collatz(2/2) now what is collatz(2/2)? 2/2 = 1 so: is 1 == 1? yes return 0 now the program will have build this stack of function calls function like this return 1 + collatz(4/2) + collatz (2/2) + 0 which is replaced by return 1 + 1 + 1 + 0 so the final result will be 3 steps.
@kasperderej7401
@kasperderej7401 4 жыл бұрын
@@iEatBass thanks homie! I couldn't get if for shit.
@jcasti01
@jcasti01 3 жыл бұрын
@@iEatBass Thank you for this explanation, I finally got it as well!
@stev_en_bee
@stev_en_bee Жыл бұрын
@@iEatBass Super helpful. I'm surprised this wasn't explained in more detail in this video, as these shorts are usually so good at spending a longer time breaking down concepts from the main lecture. This is the third or fourth resource to try to understand recursion that I've followed, and it's helping, but it's still super counter-intuitive and not sexy at all, yet.
@Kaminomenom
@Kaminomenom Жыл бұрын
@@iEatBass In your explanation, the final result should be 2 steps no? in this part "return 1 + collatz(4/2) + collatz (2/2) + 0", idk where "return 1" came from
@laur-unstagenameactuallyca1587
@laur-unstagenameactuallyca1587 3 жыл бұрын
I tried writing a collatz conjecture using recursion and mine looks VEEERY different to his. I just wonder what the logic of 1 + function() is? It doesn't seem to be adding one to the value of the outputted return value from that function?? Is it more like a... if n is initially 5 (which means 5 steps, using collatz conjecture, to 1) (1 +) collatz(n) + (1 +) collatz(n) + (1 +) collatz(n) + (1 +) collatz(n) + (1 +) collatz(n) Is it more like that above? if that even makes sense. Because I was pretty sure you couldn't return multiple values in functions unless you use pointers... or structs/arrays (apparently the latter are possible according to a website I just checked). So is the above more like returning (1 +) ____ (1+) ____ (1+)____ (1+) _____ (1+) (1+1+1+1+1) to the function before it finally returns the sum of that to be printed? Hope this made sense lol (and yes I am using the youtube comment sections as a way to have a conversation lol)
@HUNrobar
@HUNrobar 3 жыл бұрын
With recursion the program basically builds up a "call stack" until it reaches a tipping point, n = 1 in this case. Then it finishes from the inside out. So it finishes the "deepest" call first and slowly works backwards until it reaches the first call. So the code will count up from 0 one by one until the original function returns its value. Try creating something simpler first, like the factorial function and print out the states or watch it with the debugger. Imagine that you build a line of dominoes, and then you push the last one.
@bitetheapple8
@bitetheapple8 Жыл бұрын
beautifully explained. Thank you!!
@ramishanwar8989
@ramishanwar8989 Жыл бұрын
Neither the program I wrote worked for 0, nor the one shown here. I can see why, but how to solve it without giving it an else if condition?
@arthurwulfwhite8282
@arthurwulfwhite8282 5 жыл бұрын
You can do collatz(n) in a simple while loop without polluting the stack.
@RaphaelFellowes
@RaphaelFellowes 5 жыл бұрын
yes but that's not really the point is it
@souljarohill8795
@souljarohill8795 Ай бұрын
for the last wouldnt we want to pass in a counter to count the steps?
@jonnysegway7866
@jonnysegway7866 Ай бұрын
interesting question, I have just been trying to work this out i.e. where is the counter!! He doesn't explain what is going on here. but you notice he returns a 1 on each call, yeah it makes a recursive call but only actually returns 1. So each time a call is made 1 is returned. So say when, in your main() you have "printf("Steps %i ",collatz(6));" collatz(6) is replaced with 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
@danivanon
@danivanon 2 жыл бұрын
13:00 if you keep returning 1 over and over again, will it sum them up automatically?
@n.a3642
@n.a3642 11 ай бұрын
No because then you aren't using that return value in any way and always getting 1
@LiveRaidei
@LiveRaidei 3 жыл бұрын
Could anyone explain to me why the two recursive statements for the final solution have "return 1 +" at the beginning? I'm just trying to get my head around the logic of it
@rafaeleuzebio3090
@rafaeleuzebio3090 3 жыл бұрын
see call stacks: kzbin.info/www/bejne/l3SznKawmrCFl5Y
@WicCaesar
@WicCaesar 2 жыл бұрын
Because if n == 1, it doesn't count a step. But if it's higher than 1, it should count a step for each recursion. In my code, I simply incremented steps++, then called the recursion, and it worked. However, I had to declare int steps in the main function and pass it along with the user input.
@Drakonus_
@Drakonus_ 2 жыл бұрын
Because if the value becomes 1, it returns 0, which will be added to the 1 of the previous recursive call. Thus making itself count for each of the recursive calls.
@thanhgiangngoc8642
@thanhgiangngoc8642 10 ай бұрын
@@WicCaesar may I have your full code which you incremented the step++ instead of return 1 + ... ?
@intelligentrocky181
@intelligentrocky181 10 ай бұрын
function collatz(n) { console.log(n); n==1?n:n%2==0?collatz(n/2) :collatz(3*n+1); } guys from odin hai
@juliangallego7963
@juliangallego7963 3 жыл бұрын
Are you looking for recursion?
@juliangallego7963
@juliangallego7963 3 жыл бұрын
Are you looking for recursion?
@gona3665
@gona3665 3 жыл бұрын
@@juliangallego7963 Are you looking for recursion?
@Drakonus_
@Drakonus_ 2 жыл бұрын
@@gona3665 Are you looking for recursion?
@nayankumarbarwa4317
@nayankumarbarwa4317 2 жыл бұрын
​@@Drakonus_ Are you looking for recursion?
@alejaguilar
@alejaguilar 4 жыл бұрын
I don't understand the Fibonacci, you said the fourth element would be 2: (n-1)th element + (n-2)th element, Where do you get the N value from? And why are you adding the second and third element and not 1st as 2nd?
@WicCaesar
@WicCaesar 2 жыл бұрын
n - 1 is the last integer in a sequence (because an array starts counting from element [0], and so n - 2 is the next to last. Fibonacci starts with 0 and 1, which is n - 2 and n - 1, respectively. 0 + 1 = 1. So the sequence is 0, 1, 1. Now n - 2 and n - 1 are both 1. 1 + 1 = 2, so the sequence is now 0, 1, 1, 2, n -2 == 1, n - 1 == 2. 2 + 1 = 3; 0, 1, 1, 2, 3. What are n - 2 and n - 1 in this stage of the sequence?
@Hateburn
@Hateburn 2 жыл бұрын
Hello from The Odin Project.
@abdulnarimanov2256
@abdulnarimanov2256 4 жыл бұрын
i don't understand the meaning of "1 + " in the return statement. can someone explain?
@dhomini1140
@dhomini1140 4 жыл бұрын
Hi. Each time one of the recursive cases (n is even, or n is odd) is reached, a unit will be added for the total steps to reach 1 (in both (1 + n / 2) and (1 + 3 * n)). The moment that the n / 2 operations is equal to 1, the value zero will be returned, and all ones that were "collected" in each function call will be added. If you still have any questions, you can call.
@Narjod
@Narjod 4 жыл бұрын
@@dhomini1140 but isn't return 1 usually used to indicate an error?
@dhomini1140
@dhomini1140 4 жыл бұрын
@@Narjod Not um this case. The 1 os usually returned as default in the main function. In the function created in the vídeo, the 1 is returned to be added to the total number of steps
@dhomini1140
@dhomini1140 4 жыл бұрын
Hi. The execution ends when n == 1 in one of the function calls. Until this, the program is going to keep calling the function recursively. The total of "1" that is added represent the total of steps(or how many times the function was called). When the program reach the base case(n == 1) all the function call will be evaluated. The last call is going to return 0, and all the other calls will return 1. After the this, the first function call will return the total of steps(represented by the sum of all the "1" from each function return)
@ablaze7771
@ablaze7771 3 жыл бұрын
@@dhomini1140 thx bro, you help me a lot, I have been struggling to find the reason why he put +1 in the recursive case, thanks
@gofchu
@gofchu 8 ай бұрын
My head is about to explode.
@tusharkhairnar9142
@tusharkhairnar9142 4 жыл бұрын
Can we use loops in Recursive functions? if we can; what makes recursive programming different from interactive. programming
@MirzokhidMukhsidov
@MirzokhidMukhsidov 4 жыл бұрын
I think this code has mistake what if it is 0! i think it should be 1, but in this video hw didn't mentiones that
@fareslakhdhar8972
@fareslakhdhar8972 4 жыл бұрын
I suppose you have to prompt the user for a number greater than 1.
@dhomini1140
@dhomini1140 4 жыл бұрын
Hi. In the fact() function, if the n is equal 0 the program is not going to enter the while loop (because n > 0 is False) and will just return product at the end (product = 1).
@exnihilonihilfit6316
@exnihilonihilfit6316 4 жыл бұрын
9:37 It's stated the conjecture applies to positive integers. They just don't do any checks of the input - for brevity, I guess.
@edoardodepiccoli3004
@edoardodepiccoli3004 2 ай бұрын
this man beating his meat to lines of code fr, true programmer, real G
@LagrangePoint0
@LagrangePoint0 2 ай бұрын
He's a certified hood gangsta
@edoardodepiccoli3004
@edoardodepiccoli3004 2 ай бұрын
@@LagrangePoint0 like no others
@LagrangePoint0
@LagrangePoint0 2 ай бұрын
@@edoardodepiccoli3004 He's even throwing his gang's signs at 7:14
@mridhulml3269
@mridhulml3269 12 күн бұрын
Dude's dressedlike steve carrel from crazy stupid love😭
@Eric-we4xb
@Eric-we4xb 4 жыл бұрын
ill be honest. I came to this video just for the comments
@francegamer
@francegamer Жыл бұрын
He's so dreamy :3
@LagrangePoint0
@LagrangePoint0 2 ай бұрын
so chubby ^^
@francegamer
@francegamer 2 ай бұрын
@@LagrangePoint0 Yeah, you get it.
@grahamjoss4643
@grahamjoss4643 4 жыл бұрын
miss you dougie !!!
@YusufAydn1
@YusufAydn1 4 жыл бұрын
Thanks a lot.
@74dorset
@74dorset 2 жыл бұрын
Doug is bae.
@devinpadron5
@devinpadron5 Жыл бұрын
Thanks, Doug. Very helpful.
@donovanm.8909
@donovanm.8909 2 жыл бұрын
10:57 there's a typo - 'is applies' should read 'is applied' Also, why are we multiplying by 3 and then adding 1? Just add 1...it does exactly the same thing with less steps
@JonnyLovato
@JonnyLovato Жыл бұрын
I think for the sake of this demonstration, they were looking to make a scenario of * 3 + 1 so that it could add more steps to the process, in which you would have to figure out how many steps it would take.
@donovanm.8909
@donovanm.8909 Жыл бұрын
@@JonnyLovato this is an equation used all the way down into high school basic algebra. its used because they think its what should be used. its worthless and tedious when there's a better/faster way
@Kaminomenom
@Kaminomenom Жыл бұрын
@@donovanm.8909 what do you mean? the rules are if the number is odd, multiply by 3 and then add 1. How is just adding 1 the same thing
@akariamano5544
@akariamano5544 5 жыл бұрын
The code will not work if to write this for odds: else if (n % 2 != 0) ... Why?
@simonricard4403
@simonricard4403 5 жыл бұрын
I might be wrong but it might be because you're missing a set of parenthesis? I would guess the code doesn't know if it should be ((n % 2) != 0), or (n % (2 != 0)).
@barraged999
@barraged999 5 жыл бұрын
If (n % 2 == 0)
@WicCaesar
@WicCaesar 2 жыл бұрын
@@simonricard4403 I tried without parentheses and it worked for me. Must be something else.
@MrBowmanXD
@MrBowmanXD 2 жыл бұрын
Pretty good video
@alieeldeenahmed2278
@alieeldeenahmed2278 5 жыл бұрын
The last example for collatz . how he calculated no. of steps , what i understood he made recursion to let the number be 1 , and didn't calculate the steps , Am i right ? or something i miss understood it
@osmirog1936
@osmirog1936 4 жыл бұрын
I don't understand the purose of adding 1. I don't see how Doug's code calculates steps.
@osmirog1936
@osmirog1936 4 жыл бұрын
I got it! After completing the Collatz's algorithm, the collatz function in the base case will eventually return 0, so we can use its return value as a counter at this point. After that, the next function refers to the function that returned 0 and adds 1 to it. The next function takes the value of the function that returned 1 and adds 1 to it, and so on in recursion. The number returned by the last function will be the total number of functions involved in the recursion.
@ishan_murjhani
@ishan_murjhani 3 жыл бұрын
can someone explain, This is the error msg when I run this "non-void function does not return a value in all control paths [-Werror,-Wreturn-type]"
@samantkumar457
@samantkumar457 2 жыл бұрын
keep adding a return statement with some value at the end of each if or else ( make sure that the return is not reached in your code)
@ezzenious9923
@ezzenious9923 2 жыл бұрын
I think that happens if you lock all of your returns behind if conditions, which is why the example code just checks for even numbers and just uses an else statement instead of else if. Also the elses aren't actually necessary since the returns will skip the elses anyway.
@Drakonus_
@Drakonus_ 2 жыл бұрын
You need to put a return value outside of an if-statement, correct me if I'm wrong.
@jen-lichen8163
@jen-lichen8163 4 жыл бұрын
Thanks Doug! Nice talk as usual. Just curious, for the Collatz function, may I use "break" in the first condition (if n == 1), or, there is not actually such use in C? In my code, I used break for "if n==1" and I added a "return 0" after "else".. I am new in C by the way.
@seeknndestroy420
@seeknndestroy420 4 жыл бұрын
if you break without return anything, program will not execute if you input 1 to it. But it better gives the value 0 back to the user, meaning of 0 step needed if we have the number 1 already. Return function basically break after it gives a value back, so its more meaningful to use return there
@jen-lichen8163
@jen-lichen8163 4 жыл бұрын
@@seeknndestroy420 Thank you very much!! It's helpful!
@perscillamusonda8310
@perscillamusonda8310 5 жыл бұрын
good explanation!
@liamparrish2619
@liamparrish2619 7 жыл бұрын
for odd number why can't you just use n+1 instead of 3n+1? I don't see why multiplying by 3 is necessary
@douglloyd6811
@douglloyd6811 7 жыл бұрын
Afraid I didn't make up the Collatz conjecture, I'm only the messenger...and not good enough at theoretical mathematics to tell you why it's 3n and not n! :) en.wikipedia.org/wiki/Collatz_conjecture
@javaexpertsa8947
@javaexpertsa8947 6 жыл бұрын
n+1 would be wrong. Take 1 for n and we get 2 and 2 ain't odd. If you want make it a odd number it should be 2n+1. Take any number for n and you get a odd number. Also 3n+1 is wrong, if you choose 1 for n, you will get 4. 4 is a even number. 😬
@jamblamb
@jamblamb 5 жыл бұрын
@@javaexpertsa8947 what are you talking about? If n is 1, you follow the first step, and stop...
@rautermann
@rautermann 4 жыл бұрын
It's the simplest way to make sure we don't end up in a loop somewhere along the way. The conjecture is: Every positive integer will get to one eventually with these simple steps. It might blow up in the process, though!
@nayankumarbarwa4317
@nayankumarbarwa4317 2 жыл бұрын
@@javaexpertsa8947 ???... Makes 0 sense
@cwejter
@cwejter 4 жыл бұрын
i was trying to keep it simple and run it with get_int and printf but i keep getting error: function definition is not allowed here its at the first "{" after I started int collatz (n) any thoughts?
@jaysankhe8350
@jaysankhe8350 4 жыл бұрын
can you post the code?
@cwejter
@cwejter 4 жыл бұрын
@@jaysankhe8350 sure thing, I've simplified it a little bit, but still no luck: #include #include #include int main (void) { int n = 5; int collatz (n) { if (n == 1) return 0; else if ((n %2) == 0) return 1 + collatz(n/2); else return 1 + collatz(3n + 1); } printf("number of steps: %i", collatz); }
@dirtykimono4874
@dirtykimono4874 4 жыл бұрын
@@cwejter Hi! If the question is still relevant, I suggest you didn't define your function. You should first include before int main (void) a line of code, which will be a declaration of the function and after the main part you should write a definition. So, implented in a programm, it will look something like: #include #include #include int collatz(int n); int main (void) { int n = get_int("Give me an integer: "); int steps = collatz(n); printf("number of steps: %i ", steps); } int collatz(int n) { { if (n == 1) return 0; else if ((n % 2) == 0) return 1 + collatz(n / 2); else return 1 + collatz(3 * n + 1); } }
@cwejter
@cwejter 4 жыл бұрын
@@dirtykimono4874 damn I'm dumb. Thanks :D !
@tejasbele1456
@tejasbele1456 4 жыл бұрын
Awesome
@kurrizzle
@kurrizzle 8 ай бұрын
Hold on... were ya'll looking for kzbin.info/www/bejne/o6uZpXSAgrugnK8?
@lacelilies
@lacelilies Жыл бұрын
omg saw a whole nother side of doug once he called it sexy
Call Stacks - CS50 Shorts
7:40
CS50
Рет қаралды 127 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 19 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 23 МЛН
Pointers - CS50 Shorts
29:18
CS50
Рет қаралды 206 М.
This is a Better Way to Understand Recursion
4:03
Alex Hyett
Рет қаралды 60 М.
Binary Search - CS50 Shorts
9:32
CS50
Рет қаралды 133 М.
Tries - CS50 Shorts
16:24
CS50
Рет қаралды 74 М.
Python - CS50 Shorts
39:01
CS50
Рет қаралды 137 М.
2 Years of C++ Programming
8:20
Zyger
Рет қаралды 2,9 М.
Hash Tables - CS50 Shorts
18:47
CS50
Рет қаралды 149 М.
Functions - CS50 Shorts
20:25
CS50
Рет қаралды 106 М.
CS50 Fall 2024 - Artificial Intelligence (live, unedited)
1:01:20
Dynamic Memory Allocation - CS50 Shorts
14:12
CS50
Рет қаралды 93 М.