|
-
June 20th, 2006, 06:44 PM
#1
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.
-
June 20th, 2006, 06:52 PM
#2
Re: Find a Sum of a series
 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...
-
June 20th, 2006, 06:59 PM
#3
Re: Find a Sum of a series
But how ?
-
June 20th, 2006, 07:02 PM
#4
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
-
June 20th, 2006, 09:13 PM
#5
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..
-
June 20th, 2006, 09:21 PM
#6
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.
-
June 20th, 2006, 10:48 PM
#7
Re: Find a Sum of a series
thanks everyone, you know so much
-
June 21st, 2006, 02:17 AM
#8
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
-
June 21st, 2006, 04:26 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|