DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

TermxRecursion.cpp

02.01.2008
| 986 views |
  • submit to reddit
        #include <iostream>

using namespace std;

float term(float x, int n)
{
      if (n==0)
        return 1;
      else
        return -x*term(x,n-1);
}

float logr(float x, int n)
{
      if (n==3)
         return 0;
      else
          return term(x,n)/(float)n + logr(x, n+1);
}

float log(float x)
{
      return logr(x-1,1);
}

main()
{
      cout << term(1,1) << " " << term(3,1) << endl;
      cout << log(1.0) << " " << log(2.71828) << endl;
      getchar();
}