Thread: Factorial operator !

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Unhappy

    Quote Originally Posted by manutd
    Thanks for all the replies. I already have a function that works, but I was hoping I could make the notation cleaner. Ah well.
    Easy! DON'T overload the logical negation operator for other purposes!

    Use a function called "Factorial".

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Faster algorithm: given the numbers x[0]... x[n-1], recursively get the product of (x[0], x[2] ... ) and (x[1], x[3]... ), multiply the two numbers and return the result. Of course, this is only worthwhile for large numbers with good multiplication algorithms.

    Faster: (not perfectly accurate result) Stirling's formula.


    Chaos: why not try use macros and do all calculations at compile time as well...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #18
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    iMalc: That's what I have, using recursion. I just wanted to use ! instead of factorial().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #19
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by jafet
    Chaos: why not try use macros and do all calculations at compile time as well...
    1. templates are type-safe and respect namespace scope
    2. even if you use a macro it's not guarenteed to do the calculation at compile time.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #20
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You're all missing the point. manutd, using the function makes the notation cleaner. It doesn't make sense to overload operators with weird behaviors. The ! operator is for logical negation, and you wouldn't use it for factorial any more than you would use + and * to print things out. If you want a postfix factorial, try Haskell (only with GHC) or Perl 6. And the people obsessed about efficiency? Your conversation is off topic and already over-discussed, but I'd like to thank the people who gave examples of ways to misuse the template system.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #21
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    >> but I'd like to thank the people who gave examples of ways to misuse the template system.


    It's not a "misuse" of the template system. Template meta-programming is a perfectly valid C++ technique. Entire libraries have been written around it by people a lot smarter than me or you. look up Boost.MPL if you don't believe me. There's more to programming than the imperative c-style you're used to....
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > You're all missing the point.

    No. We aren't. It was adressed before. You just decided to not read the thread fully.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #23
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    @Rashakil Fol: If you read my post, I've already done that. And using ! as a factorial operator does make sense, that is the standard notation. Unfortunately, it is not possible for technical reasons. I understand this.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #24
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just kidding. But I've seen a prime number generator in the preprocessor before... though you could point out a similar one using meta-templates.

    but I'd like to thank the people who gave examples of ways to misuse the template system.
    Aw, shucks... [blushes] you should really thank these folks instead.

    manutd: Might as well try operator^ for exponentiation as well, eh. If it shouldn't be done, there usually isn't any point in doing it.
    Last edited by jafet; 11-11-2006 at 03:59 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM