Easy! DON'T overload the logical negation operator for other purposes!Originally Posted by manutd
Use a function called "Factorial".
Easy! DON'T overload the logical negation operator for other purposes!Originally Posted by manutd
Use a function called "Factorial".
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;}
iMalc: That's what I have, using recursion. I just wanted to use ! instead of factorial().
1. templates are type-safe and respect namespace scopeOriginally Posted by jafet
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?
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.
>> 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?
> 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.
@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.
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.
Aw, shucks... [blushes] you should really thank these folks instead.but I'd like to thank the people who gave examples of ways to misuse the template system.
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;}