Moods
Moods
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
honestly I'm not sure
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
i just did NOT think of it
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
great question, a for-loop is better
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
yeah why didn't i
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
actually
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
oh
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
and continue
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
next number 0 + 1
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
then it starts
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
it's 0, 1 as the first two numbers
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
yeah i could have made the array outside the method and put it as a class field. That is actually my preferred way to do stuff like this cus it makes it clearer for me
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
that's how it is done bottom-up
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
okay it is <
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
it may be <= instead of < but i am not on vscode and im too stupid to do it in my head
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
public int FibButBottomUp(int nth_number)
{
if(nth_number == 0) return 0;
if(nth_number == 1) return 1;


int first_num = 0, second_num = 1;
int count = 2;

while(count < nth_number)
{
int temp = first_num + second_num; //getting the next number
first_num = second_num;
second_num = temp;

count++;
}

return second_num;
}
public int FibButBottomUp(int nth_number)
{
if(nth_number == 0) return 0;
if(nth_number == 1) return 1;


int first_num = 0, second_num = 1;
int count = 2;

while(count < nth_number)
{
int temp = first_num + second_num; //getting the next number
first_num = second_num;
second_num = temp;

count++;
}

return second_num;
}
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
like with a loop
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
i think this is also top-down approach...i am not 100% sure but that should be the term, for bottom-up approach instead of doing recusively + using caching you'd just do it completely iteratively
33 replies
CC#
Created by vl4do on 7/31/2024 in #help
✅ Fibonacci sequence
while the first code snippet is right(and even sorta optimal)...that's a pretty bad way to explain the point of memoization Memoization exists for situations where there is redundant calculations being done. for example here,
public int Fib(int nth_number)
{
if(nth_number == 0) return 0;
if(nt_number == 1) return 1;

return Fib(nth_number - 1) + Fib(nth_number - 2);
}
public int Fib(int nth_number)
{
if(nth_number == 0) return 0;
if(nt_number == 1) return 1;

return Fib(nth_number - 1) + Fib(nth_number - 2);
}
If i used nth_number as something like 5, it'd start off as a call to Fib(5), then Fib(5) calls (case a)Fib(4) and Fib(3) for case a: Fib(4) calls Fib(3) and Fib(2) Fib(3) calls Fib(2) and Fib(1), then Fib(2) calls Fib(1) and Fib(0) --ends here Fib(2) (from the Fib(4) call) is done again (an example of a redundant call as Fib(2) was already done) for something like Fibonacci it wouldn't matter too much to have redundant calls, but for situations similar in nature to it with stuff like expensive calculations in each function call, it pays to have redundant calls. That's where memoization comes in, it caches(stores) each call once completed so that if this call were to happen again it would not need to do the expensive calculations again, it just returns the cached call. A way to properly implement memoization in Fibonacci would be
public int Fib(int nth_number)
{
//all the elements in the array are 0
int[] memo = new int[nth_number];
memo[0] = 0; //first number is always zero
memo[1] = 1; //second number is always one

//im assuming nth_number is never negative
if(nth_number < 2) return memo[nth_number];

return Helper(nth_number, memo):
}

private int Helper(int n, int[] memo)
{
if(memo[n] != 0) return memo[n];
else{
memo[n] = Helper(n - 1, memo) + Helper(n - 2, memo);
return memo[n];
}
}
public int Fib(int nth_number)
{
//all the elements in the array are 0
int[] memo = new int[nth_number];
memo[0] = 0; //first number is always zero
memo[1] = 1; //second number is always one

//im assuming nth_number is never negative
if(nth_number < 2) return memo[nth_number];

return Helper(nth_number, memo):
}

private int Helper(int n, int[] memo)
{
if(memo[n] != 0) return memo[n];
else{
memo[n] = Helper(n - 1, memo) + Helper(n - 2, memo);
return memo[n];
}
}
33 replies
CC#
Created by engineertdog on 7/31/2024 in #help
Parallel list
4 replies
CC#
Created by engineertdog on 7/31/2024 in #help
Parallel list
Task.WhenAll is fine yeah(btw unless I'm blind TransformRecordsParallel is never called in your code)
4 replies