
/**///////////////////////////////
//Prime.cpp
/**///////////////////////////////
template<int Val>
struct IntType

{
const static int value = Val ;
};
template<bool flag, typename T, typename U>
struct Select

{
typedef T Result;
};
template<typename T, typename U>
struct Select<false, T, U>

{
typedef U Result;
};
template <unsigned int N,unsigned int x>
struct FindRoot

{
const static int value=Select<(N/x)==x||((N/x+x)/2==x),IntType<x>,FindRoot<N,(N/x+x)/2> >::Result::value;
};
template <unsigned int N>
struct Sqrt

{
const static int value=FindRoot<N,N/2>::value;
};
template <>
struct Sqrt<0> ;
template <int N,int divider>
struct TestPrime

{
const static int value=Select<(N%divider)==0,IntType<0>,TestPrime<N,divider-1> >::Result::value;
};
template <int N>
struct TestPrime<N,1>

{
const static int value=1;
};
template <unsigned int N>
struct IsPrime

{
const static int value=TestPrime<N,Sqrt<N>::value+1>::value;
};
template <>
struct IsPrime<2>

{
const static int value=1;
};
template <>
struct IsPrime<1>;
int printf(const char*,
);
int main()

{
const int yes=IsPrime<123127>::value;
printf("%d\n",yes);
}
| 只有注冊用戶登錄后才能發表評論。 | ||
|
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
|
||
|
相關文章:
|
||
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|
|