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;
}
I'm assuming you expect it to work for all positive integers not in A023108?
– Mr. Llama – 2012-03-08T22:58:48.537@GigaWatt It should at least work for all positive integers that fit in the <integer> type of the language that you use. – Eelvex – 2012-03-09T05:50:21.200
@Eelvex - So... when the input is
196
, what's the expected output? It fits in the integer data type quite comfortably. – Mr. Llama – 2012-03-09T15:39:38.947@GigaWatt the expected output in this case is the palindrome reached with input
196
, but since we wouldn't want to wait forever, I won't ask you to try it. – Eelvex – 2012-03-09T16:02:14.3071@GigaWatt also, I had missread your fist question :) Just don't bother with A023108s' case. – Eelvex – 2012-03-09T16:03:44.943
@Eelvex: How will Lychrel numbers be handled? – Joel Cornett – 2012-05-27T12:36:42.390
1@Joel, as with A023108, just ignore them (act like you don't know about them); we don't know if any exists anyway. – Eelvex – 2012-05-27T15:02:16.663
@Nakilon please give a reason for your edit. – Eelvex – 2011-01-29T08:31:09.733
6Because your question is probably the only one involving the 196 algorithm. Making single-use tags is not useful. – Chris Jester-Young – 2011-01-29T08:48:48.703
@Chris: It's a private beta with 34 questions so far; single-use tags is a normal thing at this point. Anyway, let's leave it as it is for now, retagging later is always an option :) – Eelvex – 2011-01-29T09:07:29.640
2What I meant was, your question is likely to be the only one ever to involve this topic, even in 2 years' time. :-) – Chris Jester-Young – 2011-01-29T09:12:34.267
1@Chris: Well, 196-algorithm is a pretty popular one, going by many different names. Just to be sure, though, I'll post another question about it before the 2-year-time lapses ;) – Eelvex – 2011-01-29T09:49:42.537