β I'm rly bad at recursion
we've started w Recursion abt 2 weeks ago and I'm rly struggling w it. idk at what exactly bc I'm bad at the basics but I can give u an example from my hw
107 Replies
I mean, recursion is really just "a function that calls itself"
This isn't an SMS btw, you're not limited to 140 characters
Recursion can be hard to wrap your head around, but it's essentially another way to loop
Angius
REPL Result: Success
Console Output
Compile: 765.456ms | Execution: 46.475ms | React with β to remove this embed.
Here's a very useless recursive function
but it's so complicated
Aight, what's complicated about my example?
urs is simple
I understand it
but I doubt I'd be able to write it myself
If you write complicated code, obviously it'll be complicated.
it's not even that complicated
Recursion is very easy to understand.
it is simply a function that calls itself.
the first question in my hw is to code a function that gets an int num and returns the sum of all the nums 0-num but I find it so difficult to even understand how to do
1 sec I'm sending my code
.
public static int Sum1Ton(int n)
{
if(n/10 == 0)
return;
Sum1Ton(n-1) return n; } like idk what to do
Sum1Ton(n-1) return n; } like idk what to do
can u write this itrative? (basically with loops)
Posting Code Snippets
To post a code snippet type the following:
```cs
// code here
```
Notes:
- Get an example by typing
$codegif
in the chat.
- Change the language by replacing cs
with the language of your choice (for example sql
or cpp
).
- If your code is too long, you can post it to https://paste.mod.gg/ and share the link.no
what i meant, is can u write it not recursive (a for loop would be preferable)
from there we could start to work out the recursive version
oh ok
(but i need a quick ciggy break, so ill be back in 5)
that description ‴οΈ
and ur code do different things
oh right
so do u need to get the sum of digits of a given number, or just the sum of 1 to the given number
sounds like it should be the latter (even from method name)
I need the sum of all the numbers from 0 - num
idk y I coded this
okay then try to write a loop for that first (forget about recursion for now)
this is it I think
yep thats correct
keep this one in mind
we split the code a bit up, translate it to recursion and then wire it together again
I think this is the type of code that u go to the end and then count on the way back
thats correct (i was about to type that)
so it'd be return n+i or smth like that
so basically ur for loop would have to do it in reverse order, where
i
starts with n
, can u write that as for loop?I think so
then show some code ;p
?
wait sorry
to be honest, because u wrote it in reverse order now (which is correct), i saw a bug in ur earlier loop π
lets fix that one quickly
lets say
n
is 2,
so the result should be 1 + 2, so its 3
now think about step by step what that for loop doesur right
it goes up so it counts over n
nope
it stops the moment
i
is equal to n
I understand
so what would have to change?
n+ smth
not +1
where, what? show me the complete code ;p
I'm thinking whether n+n+1 is always true
n + n + 1
is a number, so its neither true nor false, its just a number
always works
let's say n = 3
so it'd be 1+2 +3 which is less than n+n+1 which is 7
n+n maybe I'm thinking
step away from trying to do it without the for loop (its wrong btw), the for loop is crucial for the next steps anyway
I'm talking abt the i < n in the loop
ah
thats the erroneous point yes
if
n
is 2, which values would i
have within the loop?0+1+2
as in what would this print
and this is where it goes wrong
0
1
2
nope
2 < 2
is not true, so it wont execute if i
is 2
right
so that condition should be? π
or <=
yep
<=
would be preferable hereic
its because
i <= n
is better readable/understandalde than i < n + 1
yeah
there is also another minor thing, u start with
i = 0
, but u go from 1 to n
so it should be i = 1; i <= n; i++
mhm
so with the fixes to the old for loop, β€΄οΈ is now completely equal in behaviour aside that it goes in reverse order, right?
and i get a phone call ... ill make it short
I think so
it is ;p
so, lets assume
n
is 3 for our brain exercise now ;p
thats basically 3 + 2 + 1
, right?right
and if
n
would be 2
, it would be 2 + 1
?yes
so
Sum(3) = 3 + 2 + 1
and Sum(2) = 2 + 1
?
(im lazy i write Sum
for Sum1Ton
)yes
so that also means that
Sum(3) = 3 + Sum(2)
right?ohhh
that's the recursion then
yep
so if we dunno that
n
is 3, how would u write ‴οΈ
as in, we just know that n
is a numbersum(n) = n + sum(n-1)
correct
can u write this as a c# method?
the recursive method?
just this formula
$code
Posting Code Snippets
To post a code snippet type the following:
```cs
// code here
```
Notes:
- Get an example by typing
$codegif
in the chat.
- Change the language by replacing cs
with the language of your choice (for example sql
or cpp
).
- If your code is too long, you can post it to https://paste.mod.gg/ and share the link.no idk what to do
ill write the code, i have no clue how to explain it:
but won't this go forever?
yep
and thats where we are back to the for loop
if (n/10 == 0 )
return;
we cant just blindly recursively call over and over again, we have to add an condition
nope, thats something completely different.
it didint appear in the for loop did it?
no
in the for loops it was i <= n
that was the first for loop, im talking about the second
because we go in reverse order (we start with
i = n
)n > 0?
yep
basically for
n
as 2 it would currently do 2 + 1 + 0 + -1 + -2 ...
from the thinking this is correct.
but a return value is expected
n?
0
2 + 1 + 0
u r at the last number here, when n
is 0
, u know that the sum can only be 0
right
and for what its worth, we know that
something + 0
is still something
, right?correct
and we also know that sum(1) = 1, right?
ye
so now we have an explicit stopping condition, if
n
is 1
, it can only result in 1
, no need to recursively call for sums of lower numbers
can u adjust this code for β€΄οΈ ?
I don't understand if it replaces the 0 or not
its correct
and it will never reach the point where
n
is 0ye
if i tell ya to count down from any number greater than zero and i shout stop if u reach one, u never count 0
mhm
before i try to summarize, do u have any questions?
no I understand
kk
If you understand English watch this video
https://youtu.be/M2uO2nMT0Bk?si=UwXAObuhedwXReMY
Kunal Kushwaha
YouTube
Introduction to Recursion - Learn In The Best Way
This is by far one of the best Introduction to #Recursion tutorial that you can watch on the internet. Recursion is overwhelming at first for a lot of folks. In this tutorial we dive through the basics, learn how to visualise problems, even the minute details, and I share with you some of the best practices to master recursion.
Take part in the...
i rewrite ur count-down-for-loop a small bit bit:
and adjust ur method:
note that this adjusted code is just for showcase, u shouldnt use this one
ye it looks more complicated
there is
i + res;
in the for loop. if u do not think about the order (if downwards or upwards) thats the same as i + Sum(i - 1)
its just a bit of execution order shenanigans, but they are more or less the same. the counting already mentioned ‴οΈ
the breaking condition i >= 1
vs i > 1
and i == 1
(this has to be split because in the recursion the not-call-the-method-again needs extra handling)
if this last part confuses u more than it helps, just forget it
this task is basically an example how to explain recursion.
and we transitioned from the iterative way to the recursive way
in the real world u want the exact opposite if possible, because it is more performant (i wont get into detail here because its more complex, lets just say that a method call is less performant and with recursion u do a lot of these)
but im glad i could help ya o7
and if u do not have further questions regarding this, please $close the threadUse the /close command to mark a forum thread as answered