039
27.01.2007, 21:43 Uhr
0xdeadbeef
Gott (Operator)
|
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<int i> struct prime_loop { static inline void run() { prime_loop<i-1>::run(); if(is_prime<i>::val) std::cout << i << ' '; } };
template<> struct prime_loop<-1> { static inline void run() { } };
int main() { prime_loop<100>::run(); std::cout << std::endl; }
|
Meta-Code nochmal optimiert. -- Einfachheit ist Voraussetzung für Zuverlässigkeit. -- Edsger Wybe Dijkstra Dieser Post wurde am 27.01.2007 um 21:46 Uhr von 0xdeadbeef editiert. |