Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For f(n) = f(n – 1) + f(n – 2^10), when 0 <= n < 2^10, f(n)=1 ,
write a function to compute f(n).(not using recursive method)
int compute_f(int n)
{
int result = 0;
...
return result
}
wondering is there any efficient way to do?
You can follow the same idea of fibonacci and do Dynamic Programming.
Pseudo code:
if n < 0:
//throw some exception
arr = new int[max(1024,n+1)]
for i = 0 to 1024:
arr[i] = 1
for i = 1024 to n+1:
arr[i] = arr[i-1] + arr[i-1024]
return arr[n]
Converting it to actual code is left for you.
Bonus: You can do it with O(1) extra space by holding an array of size 1024 and manipulating it and remembering your current place (use modolus operator) without changing the time complexity.
Here is my version, similar to the recursive version of Fibonacci.
int compute_f(int n)
{
if( n < 0)
return -1; //Error
if(n <= 1024)
return 1;
return (compute_f(n-1) + compute_f(n-1024));
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
result = getResult( 2, 5)
int getResult(int m, int n)
{
int ans;
if (m < n)
if (n <= 10)
ans = m + n;
else
ans = m * n;
else
ans = n / m;
return (ans);
}
I am stuck between 10 and 2,
does the second else apply because the second if is true? or do i still go with the first else?
For m = 2, n = 5, the first two if conditions are valid: m < n and n <= 10. Thus, ans = m + n = 7 and it's not modified later on, so we expect 7 as the answer.
This can be much easier deduced if you properly format your code (I did it for you in this case). Also, if you use {} in if/else, that's way easier and less error-prone.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I dont really understand i have written return in the end but still it gives error
CODE :
int factorial(int num)
{
int N;
if (num > 1)
{
N = (num * factorial(num--));
}
else
return N;
}
int main()
{
cout << factorial(5);
return 0;
}
ERROR : warning: control reaches end of non-void function [-Wreturn-type]
16 | }
Your issue is that you don't return anything. If you look at the flow of the program you can see that for num > 1 you do the factorial stuff and for num <= 1 you just return N. For num > 1 the return statement is never reached. This issue can be fixed by removing the else, BUT that leaves an other issue mentioned, namely that for num <= 1 N is never initialised. If you initialise it to 1 that should solve that, but as people pointed out you don't need N, you can do return num * factorial(num - 1); and simply return 1 for num <= 1. The final problem with your code is that you do num * factorial(num--). factorial(num--) will call factorial(num), when you would need factorial(num-1), because num-- is the post-decrement operator.
Other suggestions in the comments are good to heed as well, like implementing guards from integer overflow and the like.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is the equation I'm working with (it's from a past exam question that I got wrong):
void foo(float[] array, int start, int end){
if((end-start) <= 1) return;
int x = (end-start) / 5;
int y = 2*x;
int z = 4*x;
foo(array,start,start+y);
for(index = y; index <z; index++){
array[index]++;
}
foo(array,start+z,end);
}
How would I go about coming up with a recurrence equation for this?
I'm not sure of the notation I should use since the function #recurrences is depending on the value of end-start...
T(1) = 1
T(N) = ____ + ____ + _____
for notation simplicity, lets call N = end-start
then:
foo(array,start,start+y); // T(2/5 * N)
for(index = y; index <z; index++) // 2/5 * N
foo(array,start+z,end); // T(N/5)
T(N) = T(2/5 * N) + 2/5 * N + T(N/5)
is that close enough?