CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Posts
    5

    Question Find a Sum of a series

    Can you tell me how to calculate
    f(n)=f(n-1)+n^5
    f(0)=1
    without using recursive
    Thank you
    Last edited by Kawaguchi; June 20th, 2006 at 06:47 PM.

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Find a Sum of a series

    Quote Originally Posted by Kawaguchi
    Can you tell me how to calculate
    f(n)=f(n-1)+n^5
    f(0)=1
    without using recursive
    Thank you
    In a for() loop?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Jun 2006
    Posts
    5

    Re: Find a Sum of a series

    But how ?

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Find a Sum of a series

    Maybe like this:
    Code:
    int func(int n)
    {
        int res = 1;
        while (n > 0)
        {
            res += n*n*n*n*n;
            n--;
        }
        return res;
    }
    - petter

  5. #5
    Join Date
    Dec 2005
    Posts
    382

    Re: Find a Sum of a series

    As VladimirF said. A for loop. Complete untested ..

    Code:
    template< typename T >
    std::vector < T > 
    sum_of_series ( std::vector < T >& in ) 
    {
       std::vector < T > out;
       typedef std::vector < T >::size_type  size_type;
       size_type const sz = in.size ();
       if ( ! sz  ) 
        //  throw ();  // do something ..
    
       out.resize ( sz );
       out [  0 ] = 1;
       for ( size_type n( 1 ); n< sz; ++n) 
       {
          out [ n ] = in [ n - 1 ] + ::pow ( n, 5 ) ; 
       }
       return ( out );
    }
    You could even modify code to do things inplace..

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Find a Sum of a series

    ...or build a lookup table for performance.

    Code:
    int series[] = {1, 2, 34, 277 /* more data here */};
    
    result = series[2];  // Result of  f(2).
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  7. #7
    Join Date
    Jun 2006
    Posts
    5

    Re: Find a Sum of a series

    thanks everyone, you know so much

  8. #8
    Join Date
    Sep 2004
    Posts
    519

    Re: Find a Sum of a series

    Also possible to calculate the series in one go:

    Code:
        int f(int i)
        {
            return
                static_cast<int>(
                    + 2 * std::pow(
                            i + 1,
                            6)
                    - 6 * std::pow(
                            i + 1,
                            5)
                    + 5 * std::pow(
                            i + 1,
                            4)
                    - 1 * std::pow(
                            i + 1,
                            2)
                    ) / 12 + 1;
        }
    Hope this helps

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Find a Sum of a series

    Just for the fun. You can use recursion in Meta-template programming to let the compiler generate the value for you as long as recursion in compile-time is okay as compare to recursion in run-time.

    Code:
    #include <iostream>
    using namespace std;
    
    template<int n>
    class Series
    {
    public:
    	static int get()
    	{
    		return Series<n-1>::value() + n*n*n*n*n;
    	}
    };
    
    
    template<>
    class Series<0>
    {
    public:
    	static int value()
    	{
    		return 1;
    	}
    };
    
    int main()
    {
    	cout << Series<3>::value();  // f(3)
    }
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured