040
27.01.2007, 22:03 Uhr
0xdeadbeef
Gott (Operator)
|
Und um noch einen draufzusetzen:
C++: |
#include <iostream>
template<unsigned x, unsigned i, unsigned j, bool abort> struct prime_aux { static bool const val = x % i != 0 && prime_aux<x, i + j, 6 - j, (i * i > x || x % i == 0)>::val; };
template<unsigned x, unsigned i, unsigned j> struct prime_aux<x, i, j, true> { static bool const val = true; };
template<unsigned x> struct is_prime { static bool const val = x % 2 != 0 && x % 3 != 0 && prime_aux<x, 5, 2, x % 2 == 0 || x % 3 == 0>::val; };
template<> struct is_prime<0> { static bool const val = false; }; template<> struct is_prime<1> { static bool const val = false; }; template<> struct is_prime<2> { static bool const val = true; }; template<> struct is_prime<3> { static bool const val = true; }; template<> struct is_prime<4> { static bool const val = false; }; template<> struct is_prime<5> { static bool const val = true; };
template<unsigned x, unsigned next_step, bool found> struct prime_finder { static unsigned const val = prime_finder<x + next_step, 6 - next_step, is_prime<x + next_step>::val>::val; };
template<unsigned x, unsigned next_step> struct prime_finder<x, next_step, true> { static unsigned const val = x; };
template<unsigned i> struct primes { static unsigned const next_step = 6 - primes<i - 1>::next_step; static unsigned const val = prime_finder<primes<i - 1>::val, next_step, false>::val; };
template<> struct primes<0> { static unsigned const val = 2; }; template<> struct primes<1> { static unsigned const val = 3; }; template<> struct primes<2> { static unsigned const next_step = 2; static unsigned const val = 5; };
template<int i> struct primes_loop { static inline void run() { primes_loop<i - 1>::run(); std::cout << primes<i>::val << ' '; } };
template<> struct primes_loop<-1> { static inline void run() { } };
int main() { primes_loop<100>::run(); std::cout << std::endl; }
|
-- Einfachheit ist Voraussetzung für Zuverlässigkeit. -- Edsger Wybe Dijkstra Dieser Post wurde am 27.01.2007 um 22:09 Uhr von 0xdeadbeef editiert. |