# Count even steps of 196 algorithm

In the 196-algorithm one starts from an integer and then adds its reverse to it until a palindrome is reached, like this:

start -> 5280
5280 + 0825 = 6105
6105 + 5016 = 11121
11121 + 12111 = 23232
-end-


Write a function that counts how many even numbers are visited before the algorithm ends. Starting and ending numbers are also counted, so, in the example above, visited are consider the numbers: 5280, 6105, 11121, 23232 but not 825, 5016 and 12111.

Examples:

f(5280) = 2
f(56) = 1
f(59) = 1
f(89) = 13


Extended code golf rules apply: shortest number of instructions and operations (like +,-,mod,print,while,if,...) wins.

• I see fun fights coming up what exactly is a single instruction/operation in some languages ;-) – I'm having trouble already. – Joey Feb 15 '11 at 1:39
• @Joey: Yes :) but lets try not to be too competitive. – Eelvex Feb 15 '11 at 10:30
• I'd love to see an APL-solution. – FUZxxl Feb 15 '11 at 12:11
• does a ternary operation count as 1 or 2 operations? – gnibbler Feb 15 '11 at 12:14
• @gnibbler: I would say 1. – Eelvex Feb 15 '11 at 13:17

## Mathematica

5 verbs ... not sure how to count ...

k = FromDigits@Reverse@IntegerDigits[#] &;
Count[NestWhileList[# + k@# &, #, # != k@# &], _?OddQ] &


## Python - 10 operations

def f(n):
q=str(n)
p=q[::-1]
return 1-n%2+(q!=p and f(n+long(p)))

• I count these: str, ::-1, return, -, %, +, and, f(), +, longp – Eelvex Feb 15 '11 at 10:32
• @Joey, @Eelvex, 10 it is then – gnibbler Feb 15 '11 at 10:56
• how come you're using long() wouldn't int() be shorter? – st0le Feb 15 '11 at 12:01
• @st0le, yeah for normal golf but this is about minimum number of operations so I think it doesn't matter here – gnibbler Feb 15 '11 at 12:11
• Why don't you count != as an instruction – FUZxxl Feb 15 '11 at 12:33

Hey, looks like eval, for once, isn't forbidden here!

## Perl, 1 instruction

sub f { eval << 'EOT' }
my $n =$_[0];
my $r = reverse$n;
return !($n & 1) + ($r eq $n ? 0 : f($n+$r)); EOT  # D: 154 Characters, 22 Instructions alias long l;alias string s;l f(l n){l t=!(n&1);auto u=to!s(n);while(u!=to!s(retro(u))){n=to!l(u)+to!l(to!s(retro(u)));if(!(n&1))++t;u=to!s(n);}return t;}  More Legibly: alias long l; alias string s; l f(l n) { l t = !(n & 1); //3 instructions auto u = to!s(n); //2 instructions while(u != to!s(retro(u))) //4 instructions { n = to!l(u) + to!l(to!s(retro(u))); //6 instructions if(!(n & 1)) //3 instructions ++t; //1 instruction u = to!s(n); //2 instructions } return t; //1 instruction }  I'm not sure that counted instructions quite right, since that's a bit debatable, and of course, there are lots of instructions going on in the functions which get called. So, ultimately, I'm not sure how reasonable or accurate counting instructions is, but I think that 22 is the total. • The way you counted seems fair to me. – Eelvex Feb 15 '11 at 10:36 # Scheme, 18 instructions I take an instruction to be an opening paren. (define (even-numbers-from-169-algo n) (+ (if (odd? n) 0 1) (if (equal? n (num-reverse n)) 0 (even-numbers-from-169-algo (+ n (num-reverse n)))))) (define (num-reverse n) (string->number (list->string (reverse (string->list (number->string n))))))  • I think "15 instructions" is more fair. – Eelvex Feb 15 '11 at 10:35 • @Eelvex: Which ones didn't you count? (I'm not going to argue though :)) – user475 Feb 15 '11 at 17:15 • (define (list)) and probably I counted something more? I don't know... this instructions thing is a mess ... :/ – Eelvex Feb 15 '11 at 17:19 ## Windows PowerShell, 16 instructions function f($n){
@(                                   # 1
$( # 1$n % 2                           # 1
#1     1   1     2    1
for(;-join"$n"[99..0]-ne$n) {    # 5
# 1   1   1     2
$n+=-join"$n"[99..0]           # 5
$n % 2 # 1 } # ) -eq 0).Count # 2 }  Notes: • I counted the following items as one instruction: String expansion ("$n"), operators (-ne, %, !, -join, \$(), @()), cmdlets (%), keywords (for).
• I counted indexes as two instructions since it's the index [] plus the range operator ..
• I didn't count the function header.

History:

• 2011-02-15 02:49 (18) First attempt.
• 2011-02-15 02:55 (16) Got rid of the pipeline.
• I don't know about indexes... maybe you should only count the range operator. – Eelvex Feb 15 '11 at 10:40

## J, 36 characters, 7 instructions

+/@:-.@(2&|)@((+1:@.=|.&.":)"0^:a:)


I'm counting both monadic and dyadic verbs as instructions. Here's a token breakdown to make sure everything is accounted for:

• 3 monadic verbs (6 chars): -., |., ":
• 4 dyadic verbs (4 chars): +, |, +, =
• 10 conjunctions (14 chars): /, @:, @, &, @, , @., &., ", ^:
• 4 constants (6 chars): 2 1: 0 a:
• 3 parenthesis groups (6 chars)

Demonstration:

   +/@:-.@(2&|)@((+1:@.=|.&.":)"0^:a:) 5280 56 59 89
2 1 1 13


The "0 isn't strictly needed to answer the question, it's just there to make the function more J-like (i.e., able to operate on a whole array at once, as above). I kept it in as it doesn't go against my instruction count.

• J wins again? :) – Eelvex Feb 15 '11 at 17:35
• I'm at least expecting a debate-storm of whether or not conjunctions should be counted in ;) But then again until I understand how the leading entry counts its instructions, my method doesn't seem any less legitimate. – J B Feb 15 '11 at 17:39

Java Solution

int countEvenIn196(long n){
int count=0;
boolean flag=false;
while (!flag){
for (int i=0; i<(""+n).length(); i++)
if ((""+n).charAt(i) == (""+n).charAt((""+n).length()-i-1))
{flag=true;}
else {flag=false; break;}
if (n%2==0) count++;
//n+=reverse(n);
n+=(Long.parseLong(new StringBuffer(""+n).reverse().toString()));
}
return count;
}

• IDEONE: ideone.com/wZw1t – Aman ZeeK Verma Feb 15 '11 at 1:16
• 35 instructions by my count. – Joey Feb 15 '11 at 20:51
• Thnx Joey :), how do we count this btw... does it depend on JVM or OS/Kernel or Processor.. am sure all of them have their own distinct executed instructions... I stopped counting when I started thinking about my StringBuffer.reverse(). – Aman ZeeK Verma Feb 15 '11 at 21:17

f n=1-nrem2+if q/=p then f(n+read p) else 0 where q=show n;p=reverse q