• Status: Solved
• Priority: Medium
• Security: Public
• Views: 175

# Help Writing a Recursive Function

Help, i need to write a recursive function showing that the Sumation of (k-1)/2^k = 0 when k is from 0 to infinity.
hope this makes sense, if not let me know

thanks!
0
ReignMan
• 2
1 Solution

Commented:
You need to build a recursive algorithm.
Let f(k) = (k-1)/2^k
Let g(n) = S(f(k)) for k=0..n

For n > 0,
g(n) = f(n) + g(n-1)

For n = 0,
g(n) = f(n)

The above defines g(n) as a recursive function, which is trivial to implement in C.

double g(int n) {
assert(n >= 0);
if (n == 0)
return f(n);
else
return f(n) + g(n - 1);
}

double f(double k) {
return (k-1)/pow(2,k);
}
0

Commented:
hmm, you sure you got that right? I may be wrong but wouldn't (k-1)/2^k=0 have a single answer? My math skills may not be great, I'm probably wrong. But if what your looking for is x in x = (k-1)/2^k then I believe the following code should work for you. It will print the answers to a file due to the large amount of numbers involved. Running this for 1 minute creates the numbers from 0 to 100000. Heres the code:

#include <stdio.h>
#include <iostream.h>

int a = 0;
int k = 0;
double c;
double r = 0;
double f = 0;
int g = 0;
int p = 0;
int b = 0;
float ans;

void main()

{

FILE * pFile;

pFile = fopen ("myfile.txt","w");

loop1:

{

while(b <= f)

{

if (p = 0)

{

r = 2 * 2;

p = p++;

}

if (p != 0)

{

r = r * 2;

p = p++;

}

b = b++;

}

c = (k-1);

g = (2^k);

ans = c/g;

fprintf (pFile, "Variable k being %d resulted in %f\n",k,ans);

k = k++;

f = f++;

r = 0;

b = 0;

p = 0;

goto loop1;

}

fclose (pFile);

}

The answers that come out look sorta funny to me, but as I said, Im not a math guy, if it doesnt work, just tell me any I'll try to help you further. Good luck.
0

Commented:
whoa, completely ignore my ignorance, i didnt see his much MUCH better answer. Props to burtday
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.