Solved

# Help Writing a Recursive Function

Posted on 2003-03-30
Medium Priority
173 Views
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
Question by:ReignMan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 5

Accepted Solution

burtdav earned 400 total points
ID: 8235492
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

Expert Comment

ID: 8236243
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

Expert Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month9 days, 13 hours left to enroll