βœ… 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
Angius
Angiusβ€’14mo ago
I mean, recursion is really just "a function that calls itself"
Pobiega
Pobiegaβ€’14mo ago
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
MODiX
MODiXβ€’14mo ago
Angius
REPL Result: Success
void ReduceByOne(int number)
{
Console.WriteLine($"Number is now {number}");
if (number <= 0)
{
Console.WriteLine("Done");
return;
}
ReduceByOne(number - 1);
}

ReduceByOne(5);
void ReduceByOne(int number)
{
Console.WriteLine($"Number is now {number}");
if (number <= 0)
{
Console.WriteLine("Done");
return;
}
ReduceByOne(number - 1);
}

ReduceByOne(5);
Console Output
Number is now 5
Number is now 4
Number is now 3
Number is now 2
Number is now 1
Number is now 0
Done
Number is now 5
Number is now 4
Number is now 3
Number is now 2
Number is now 1
Number is now 0
Done
Compile: 765.456ms | Execution: 46.475ms | React with ❌ to remove this embed.
Angius
Angiusβ€’14mo ago
Here's a very useless recursive function
𝓭𝓾𝓼𝓴𝔂 🐸
but it's so complicated
Angius
Angiusβ€’14mo ago
Aight, what's complicated about my example?
𝓭𝓾𝓼𝓴𝔂 🐸
urs is simple I understand it but I doubt I'd be able to write it myself
Buddy
Buddyβ€’14mo ago
If you write complicated code, obviously it'll be complicated.
𝓭𝓾𝓼𝓴𝔂 🐸
it's not even that complicated
Buddy
Buddyβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
can u write this itrative? (basically with loops)
MODiX
MODiXβ€’14mo ago
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
// public static int Sum1Ton(int n)
{
if(n/10 == 0)
return;

Sum1Ton(n-1)
return n;
}
// public static int Sum1Ton(int n)
{
if(n/10 == 0)
return;

Sum1Ton(n-1)
return n;
}
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
(but i need a quick ciggy break, so ill be back in 5)
𝓭𝓾𝓼𝓴𝔂 🐸
// public static int Sum1Ton(int n)
{
int res = 0;
while (n != 0)
{
res += n % 10;
n /= 10;
}
return res;
}
// public static int Sum1Ton(int n)
{
int res = 0;
while (n != 0)
{
res += n % 10;
n /= 10;
}
return res;
}
cap5lut
cap5lutβ€’14mo ago
that description ‴️ and ur code do different things
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
okay then try to write a loop for that first (forget about recursion for now)
𝓭𝓾𝓼𝓴𝔂 🐸
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = 0; i < n; i++)
res += i;

return res;
}
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = 0; i < n; i++)
res += i;

return res;
}
this is it I think
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
thats correct (i was about to type that)
𝓭𝓾𝓼𝓴𝔂 🐸
so it'd be return n+i or smth like that
cap5lut
cap5lutβ€’14mo ago
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?
cap5lut
cap5lutβ€’14mo ago
then show some code ;p
𝓭𝓾𝓼𝓴𝔂 🐸
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = n; i > 0; i--)
res += i;

return res;
}
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = n; i > 0; i--)
res += i;

return res;
}
? wait sorry
cap5lut
cap5lutβ€’14mo ago
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 does
𝓭𝓾𝓼𝓴𝔂 🐸
ur right it goes up so it counts over n
cap5lut
cap5lutβ€’14mo ago
nope it stops the moment i is equal to n
cap5lut
cap5lutβ€’14mo ago
so what would have to change?
cap5lut
cap5lutβ€’14mo ago
where, what? show me the complete code ;p
𝓭𝓾𝓼𝓴𝔂 🐸
I'm thinking whether n+n+1 is always true
cap5lut
cap5lutβ€’14mo ago
n + n + 1 is a number, so its neither true nor false, its just a number
𝓭𝓾𝓼𝓴𝔂 🐸
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = 0; i < n; i++)
res += i;

return res;
}
// public static int Sum1Ton(int n)
{
int res = 0;
for(int i = 0; i < n; i++)
res += i;

return res;
}
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
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
ah thats the erroneous point yes if n is 2, which values would i have within the loop?
cap5lut
cap5lutβ€’14mo ago
as in what would this print
for (int i = 0; i < 2; i++)
Console.WriteLine(i);
for (int i = 0; i < 2; i++)
Console.WriteLine(i);
and this is where it goes wrong
cap5lut
cap5lutβ€’14mo ago
nope 2 < 2 is not true, so it wont execute if i is 2
cap5lut
cap5lutβ€’14mo ago
so that condition should be? πŸ˜„
𝓭𝓾𝓼𝓴𝔂 🐸
// public static int Sum1Ton(int n)
for (int i = 0; i < 3; i++)
Console.WriteLine(i);
// public static int Sum1Ton(int n)
for (int i = 0; i < 3; i++)
Console.WriteLine(i);
or <=
cap5lut
cap5lutβ€’14mo ago
yep <= would be preferable here
cap5lut
cap5lutβ€’14mo ago
its because i <= n is better readable/understandalde than i < n + 1
cap5lut
cap5lutβ€’14mo ago
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++
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
it is ;p so, lets assume n is 3 for our brain exercise now ;p thats basically 3 + 2 + 1, right?
cap5lut
cap5lutβ€’14mo ago
and if n would be 2, it would be 2 + 1?
cap5lut
cap5lutβ€’14mo ago
so Sum(3) = 3 + 2 + 1 and Sum(2) = 2 + 1? (im lazy i write Sum for Sum1Ton)
cap5lut
cap5lutβ€’14mo ago
so that also means that Sum(3) = 3 + Sum(2) right?
𝓭𝓾𝓼𝓴𝔂 🐸
ohhh that's the recursion then
cap5lut
cap5lutβ€’14mo ago
yep so if we dunno that n is 3, how would u write ‴️ as in, we just know that n is a number
𝓭𝓾𝓼𝓴𝔂 🐸
sum(n) = n + sum(n-1)
cap5lut
cap5lutβ€’14mo ago
correct can u write this as a c# method?
𝓭𝓾𝓼𝓴𝔂 🐸
the recursive method?
cap5lut
cap5lutβ€’14mo ago
just this formula $code
MODiX
MODiXβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
ill write the code, i have no clue how to explain it:
public int Sum(int n)
{
return n + Sum(n - 1);
}
public int Sum(int n)
{
return n + Sum(n - 1);
}
𝓭𝓾𝓼𝓴𝔂 🐸
but won't this go forever?
cap5lut
cap5lutβ€’14mo ago
yep and thats where we are back to the for loop
𝓭𝓾𝓼𝓴𝔂 🐸
if (n/10 == 0 ) return;
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
that was the first for loop, im talking about the second because we go in reverse order (we start with i = n)
cap5lut
cap5lutβ€’14mo ago
yep basically for n as 2 it would currently do 2 + 1 + 0 + -1 + -2 ...
𝓭𝓾𝓼𝓴𝔂 🐸
// public int Sum(int n)
{
if(n == 0)
return;
return n + Sum(n - 1);
}
// public int Sum(int n)
{
if(n == 0)
return;
return n + Sum(n - 1);
}
cap5lut
cap5lutβ€’14mo ago
from the thinking this is correct. but a return value is expected
cap5lut
cap5lutβ€’14mo ago
0 2 + 1 + 0 u r at the last number here, when n is 0, u know that the sum can only be 0
cap5lut
cap5lutβ€’14mo ago
// public int Sum(int n)
{
if(n == 0)
return 0;
return n + Sum(n - 1);
}
// public int Sum(int n)
{
if(n == 0)
return 0;
return n + Sum(n - 1);
}
and for what its worth, we know that something + 0 is still something, right?
cap5lut
cap5lutβ€’14mo ago
and we also know that sum(1) = 1, right?
cap5lut
cap5lutβ€’14mo ago
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 ‴️ ?
𝓭𝓾𝓼𝓴𝔂 🐸
// // public int Sum(int n)
{
if(n == 1)
return 1;
return n + Sum(n - 1);
}
// // public int Sum(int n)
{
if(n == 1)
return 1;
return n + Sum(n - 1);
}
I don't understand if it replaces the 0 or not
cap5lut
cap5lutβ€’14mo ago
its correct and it will never reach the point where n is 0
cap5lut
cap5lutβ€’14mo ago
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
cap5lut
cap5lutβ€’14mo ago
before i try to summarize, do u have any questions?
cap5lut
cap5lutβ€’14mo ago
kk
veryNORMAL
veryNORMALβ€’14mo ago
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...
cap5lut
cap5lutβ€’14mo ago
i rewrite ur count-down-for-loop a small bit bit:
int res = 0;
for(int i = n; i >= 1; i--)
res = i + res;
return res;
int res = 0;
for(int i = n; i >= 1; i--)
res = i + res;
return res;
and adjust ur method:
public int Sum(int i)
{
if (i > 1)
return i + Sum(i - 1);
if (i == 1)
return 1;
}
public int Sum(int i)
{
if (i > 1)
return i + Sum(i - 1);
if (i == 1)
return 1;
}
note that this adjusted code is just for showcase, u shouldnt use this one
𝓭𝓾𝓼𝓴𝔂 🐸
ye it looks more complicated
cap5lut
cap5lutβ€’14mo ago
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 thread
MODiX
MODiXβ€’14mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server