Thread: Factorial conversion

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    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; 
    }
    Last edited by micha_mondeli; 04-06-2005 at 06:35 PM.

  2. #2
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    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. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    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)?
    Last edited by micha_mondeli; 04-06-2005 at 06:35 PM.

  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    FYI, the ^ operator in C++ is used for XOR

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote 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.
    Last edited by Sang-drax; 04-07-2005 at 03:26 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    To raise a number to a power, use the pow() function in <cmath>.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM