# Thread: Factorial conversion

1. ## Compile time Math

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.

Code:
```#include<iostream>
using namespace std;

template <int n> struct Fac // general case n! = n*(n-1)!
{
enum { result = 3 ^ n - 2 ^ n }; // 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;
cout << Fac<10>::result << endl;
cout << Fac<0>::result << endl;
}```

2. surely you can just play around with this line of code:

Code:
`result = 3 ^ n - 2 ^ n`
and replace it with:

Code:
`result = 3 ^ (n - 2 ^ n)`
or would you have to include another variable to calculate "n - 2 ^ n" and then do "3 ^ VARIABLE"?

I'm a bit of a noob to programming and im learning, but i do know maths and in algebra form the top solution would be the most viable in my opinion, im just not sure if C++ allows us to use brackets in that manner

3. yeah i dont think C++ has the ^ (maybe with math.h, but idk)

it's not going to be as simple as you'd hope, but heres how you can do it:

Code:
```int iIncrement = 0;   //increment used for the while loop
int iResult3 = 3;      // the number 3 to use in power
int iResult2 = 2;      //the number 2 to use in power
int iFinalResult;       //the result after we subtract 3 - 2
int iInput;   //this is the "n" that the user [i think thats what you want?] inputted

while (iIncrement <= iInput) {   //while the increment is still less than what the user inputted
iIncrement++;      //add increment one
iResult3 = iResult3 * iResult3;    //do 3 * 3
iResult2 = iResult2 * iResult2;    //do 2 * 2
}          //it will do 3*3 and 2*2 n (or iInput) amount of times
//since, 3 ^ n is the same as 3 * 3 n ammount of times

iFinalResult = iResult3 - iResult2;  //and finally subtract the two powers```
and, as a note, the increment way i used, its probably off a number or 2, but im hoping you can fix this

4. Thanks a lot bluehead greatly appreciated
but it has to be in a template class so you can compute at compile time, and you have to instantiate with Foo<n>::result

HOw does f(n+1) relate to f(n)?

5. FYI, the ^ operator in C++ is used for XOR

6. Originally Posted by micha_mondeli
Thanks a lot bluehead greatly appreciated
but it has to be in a template class so you can compute at compile time, and you have to instantiate with Foo<n>::result
Template programming can be quite tricky, and judging from your rather naďve attempt
Code:
`result = 3 ^ n - 2 ^ n ,`
I don't think you're ready to do template programming. First, try to solve the problem using normal code. Then we will help you convert the code to template programming.

7. To raise a number to a power, use the pow() function in <cmath>.

8. Yeah go ahead and try to use ^ as exponentiation and see what happens.

This problem does not require templates so I think that is just confusing the issue.

Popular pages Recent additions