?
Solved

Help Writing a Recursive Function

Posted on 2003-03-30
3
Medium Priority
?
173 Views
Last Modified: 2010-04-15
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
Comment
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
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
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

by:AngrMgmtDdntWork
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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question