C++ TMP (256 characters)
#include<cstdio> #define Y(A,B,C,D)template<int N>struct A<C,D>{enum{v=B};}; #define Z(A)template<int N,int M>struct A{enum{v= #define n 5194 Z(R)R<N/10,M*10+N%10>::v};};Y(R,N,0,N)Z(S)S<N+M,R<N+M,0>::v>::v};};Y(S,N,N,N)main(){printf("%d",S<n+R<n,0>::v,0>::v);}
This version could be shortened a bit, but a 256-character answer is hard to pass up. Here's an un-golfed version:
#include <iostream> template<size_t N> class Reverse { template<size_t M, size_t R> struct Inner { enum { value = Inner<M/10, R*10 + M%10>::value }; }; template<size_t R> struct Inner<0, R> { enum { value = R }; }; public: enum { value = Inner<N, 0>::value }; }; template<size_t N> class OneNineSix { template<size_t M, size_t R=Reverse<M>::value> struct Inner { enum { value = OneNineSix<M + R>::value }; }; template<size_t M> struct Inner<M, M> { enum { value = M }; }; public: enum { value = Inner<N + Reverse<N>::value>::value }; }; int main() { const size_t N = 4123; std::cout << OneNineSix<N>::value << std::endl; }