recursion
so I need to make a fibonacci sequence using recursion but bro idk how u achieve that??
81 Replies
ive got this so far but thats it
you miss THE thing
recursion
well, what's the definition of the fibonacci sequence? in math terms
well
you add the previous two terms
for the new term
ya idk how to implement it
so
fib(n) = fib(n - 1) + fib(n - 2)
right?...
plus the base cases 1 and 0
i actually forgot this
how did i not think of this...
so if n = 1 or 0
i just return 0 right
just make sure to add 0 check so you won't have stack overflow
Wikipedia says fib(0) = 0 and fib(1) = 1
mkay
Starting from 0 and 1, the sequence begins[3] 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ....< 0 is undefined, I'd throw an exception for that unless you were told you can assume n >= 0
I wasn't but they didn't ask me to have validation so I'm gonna save myself the time and just not..
so is this fine?
im ngl im barely understanding my own code
the
()
on the return
are unnecessary
same for the == true
on the if
I, personally, would write it like this*
*if it weren't for the print
I would return directly from the if/else if/else, also would use string interpolation instead of formatting placeholders, but you might not be allowed to use thoseidek what that is! but thanks
@'brella boy which part are you having trouble understanding?
fib = FibRecurse(n - 1) + FibRecurse(n - 2);
like i get that
the input is having 1 minused
then 2 minused
then you add that
but like
lets say i input 3
im gonna get 3 right???
isnt that.. incorrect??
or am i misunderstanding
idk i cant wrap my head around this
and i kinda need to since im gonna have to do it iteratively after
it gives 2, not 3
its not (3-1) + (3-2)..?
fib(3) = fib(2) + fib(1)
fib(1) is already defined 1, so fib(2) + 1
fib(2) = fib(1) + fib(0)
fib(0) is already defined as 0, so
fib(2) = 1 + 0 = 1
fib(3) = 1 + 1 = 2
the function is calling itself
OH
so essentially it just repeats until it reaches the base case
yes
and then it adds those values together
oh
im really slow wow
this would just be
fib = (n - 1) + (n - 2)
which is not the Fibonacci sequenceso one small issue...
i kinda have to do it iteratively now
i tried this
but nothing happened
i despise and do not understand lists so thats probably the issue
were you told to use an array?
nope i wasnt told to use anything but i was given a template
which was everythig except the for loop
i added that myself
so i assume i have to do it in that way
:harold: you don't need a list for an iterative solution, why would they make you do that
anyhow, look at the variable you're using in the array indexes in the for loop body
idk man..
im NOT supposed to use x?
bro this is just it i HATE lists i cant visualise them i cant work with them whenever i employ them its WRONG
but i know the syntax so atp its just me
well,
x
isn't changing during the course of that loop right?
so you're setting the same array index on every iterationOOOH
x++
right
wait
no
uh
...?
no,
x
is the fib number you're looking forso...
uh
there's a second int variable in that code snippet
int[100]??
or i
i
right
so.. this?
you're still setting [x] every time
oops
so this?
what does this even do 😭
try it and see
mk
int[] fiblist
delcares a variable fliblist with a type of int array
new int[100]
makes an int array of size 100
and then the = assigns that new array to fiblistthis is new
so fiblist is an array of size 100
mk
that was throwing me off
ty
yes, with indicies from 0 to 99 (since arrays start from 0)
wait so whats the problem with this
is it the fact that the array is only size 100
for (int i = 0
your index var starts with 0
but in the loop body, you try to index into i - 1
and i - 2
, which is -1 and -2oh
those are not valid indicies, so you get an IndexOutOfRangeException
so i start with 2
yes, you already set what 0 and 1 are anyhow
ok im trying it again
moment of truth
(in case you haven't been taught about exceptions, it's how C# deals with errors, your course will cover them eventually if it's any good)
it does
surprisingly
but anyways
i think it worked
let me send the full program
i didnt before because i dont know what it does 💀
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/and these r my outputs
I've been enjoying reading this and I won't step on Bewares toes, but.. I would suggest that you kind of set a standard of how you want to write and then stick to it.
Like either don't includes
{}
for single lines or do.
EG you use 2 different styles here
It'll help you with reading your own code. If you're new I suggest just always including the {}your functions appear to disagree with each other
oh thats cause i literally didnt write the bottom half
its a template
yeah thats what im not getting
did that sound rude? sorry
No no it's fine, I should have been more attentive
I think you want
fiblist[x]
instead of x - 1
for the iterative
yeahfunny cause that was a part of the template but ill try it 😭
because right now your array is set up to have index n = fib term n
but you're asking for term n - 1 at the end
YOURE A GODSEND 🙏
THANK YOU
one last favour though
wait no nvm
i think i actually understand for once
the two indexes before the index at i
get added together
oh right i know whats confusing me
how comes returning x works if i was working with i the entire time
when you were working with
i
in that for loop, you were filling up the array with the fibonacci terms
but x
is the term you were looking for
i'm surprised they made you use an array for the iterative solution thoOOOH
SO I FILL UP THE ARRAY USING I
AND THEN USE X TO ACTUALLY FIND THE POINT IN THE SEQUENCE!!
or rather
the array
thank u!!
once again
Console.WriteLine("Iterative {1}th Fib={2}, Elapsed={0}", sw.Elapsed, x, fib);
I didn't know you could do this
Is there a reason for using this over just passing them directly in? Genuinely curious
Like this?
Console.WriteLine("Iterative {x}th Fib={fib}, Elapsed={Elapsed}");
did you mean
$"..."
?
string interpolation
in any case, that's how you did it before C# 6 added string interpolationI didn't notice there was no $ on it
But yeh
Oh I see, interesting