Advertisement
zhangsongcui

isPrime?

Apr 14th, 2011
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. //Usage: g++ isPrime.cpp -std=c++0x -DmaxNumber=XX
  2.  
  3. template <int Num, int Now=Num-1>
  4. struct isPrime
  5. {
  6.     const static bool Ask =
  7.         Num%Now==0 ? false : isPrime<Num, Now-1>::Ask;
  8. };
  9.  
  10. template <int Num>
  11. struct isPrime<Num, 1>
  12. {
  13.     const static bool Ask=true;
  14. };
  15.  
  16. template <int Num>
  17. struct Try
  18. {
  19.     Try<Num-1> Fake;
  20.     static_assert(!isPrime<Num>::Ask, "is a prime number.");
  21. };
  22.  
  23. template <>
  24. struct Try<1>
  25. {
  26. };
  27.  
  28. int main(int argc, char* argv[])
  29. {
  30.     //enum {maxNumber=10};
  31.     Try<maxNumber>();
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement