![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 11
Rep Power: 0
![]() |
FActorial Conv. Progr
Hey guys I'm wondering how I could convert this to one that computes 3^n - 2^n....that is 3 to the power n minus 2 to the power n. I understand you can't use ^ for power in c++ is that right..I'm coming from a Java background this is kinda new to me. So i'm wondering how to convert this factorial to the function above....
#include<iostream>
using namespace std;
template <int n> struct Fac // general case n! = n*(n-1)!
{
enum { result = n * Fac<n-1>::result }; // constant
};
template <> struct Fac<0> // base case 0! = 1
{
enum { result = 1 }; // constant
};
int main()
{
// Fac<c>::result is now a compile-time constant!
cout << Fac<5>::result << endl; // = 5! = 120
cout << Fac<10>::result << endl; // = 10! = 3628800
cout << Fac<0>::result << endl; // = 0! = 1
} |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
To find the power of something, you use the pow function, found in math.h. Here's an example: http://www.cplusplus.com/ref/cmath/pow.html.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 11
Rep Power: 0
![]() |
#include<iostream>
using namespace std;
template <int n> struct Fac // general case n! = n*(n-1)!
{
enum { result = 3 ^ n - 2 ^ n}; // constant
};
int main()
{
// Fac<c>::result is now a compile-time constant!
cout << Fac<5>::result << endl;
cout << Fac<10>::result << endl;
cout << Fac<0>::result << endl;
}OK what now?? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|