Count even steps of 196 algorithm

3

1

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.

Eelvex

Posted 2011-02-15T00:22:45.397

Reputation: 5 204

3I see fun fights coming up what exactly is a single instruction/operation in some languages ;-) – I'm having trouble already. – Joey – 2011-02-15T01:39:39.260

@Joey: Yes :) but lets try not to be too competitive. – Eelvex – 2011-02-15T10:30:48.847

I'd love to see an APL-solution. – FUZxxl – 2011-02-15T12:11:18.807

does a ternary operation count as 1 or 2 operations? – gnibbler – 2011-02-15T12:14:00.767

@gnibbler: I would say 1. – Eelvex – 2011-02-15T13:17:52.183

I find the link http://assemblyrequired.crashworks.org/2009/01/04/fcmp-conditional-moves-for-branchless-math/ interesting!

– Aman ZeeK Verma – 2011-02-15T15:03:50.480

if f(5280) visits 5280, 6105, 11121, 23232 seems f(5280) should be 4, and not 2. – Dr. belisarius – 2011-02-15T17:29:14.140

@belisarius: we are interested in even numbers only; 5280 and 23232 in this list. – Eelvex – 2011-02-15T17:31:31.060

@Eelvex tnx, sorry, I misread – Dr. belisarius – 2011-02-15T17:57:15.213

Answers

1

Mathematica

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

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

Dr. belisarius

Posted 2011-02-15T00:22:45.397

Reputation: 5 345

6

Python - 10 operations

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

gnibbler

Posted 2011-02-15T00:22:45.397

Reputation: 14 170

I count these: str, ::-1, return, -, %, +, and, f(), +, longp – Eelvex – 2011-02-15T10:32:53.823

@Joey, @Eelvex, 10 it is then – gnibbler – 2011-02-15T10:56:14.753

how come you're using long() wouldn't int() be shorter? – st0le – 2011-02-15T12:01:48.740

@st0le, yeah for normal golf but this is about minimum number of operations so I think it doesn't matter here – gnibbler – 2011-02-15T12:11:43.440

Why don't you count != as an instruction` – FUZxxl – 2011-02-15T12:33:42.377

4

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

J B

Posted 2011-02-15T00:22:45.397

Reputation: 9 638

3

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.

Jonathan M Davis

Posted 2011-02-15T00:22:45.397

Reputation: 705

The way you counted seems fair to me. – Eelvex – 2011-02-15T10:36:33.647

2

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))))))

user475

Posted 2011-02-15T00:22:45.397

Reputation:

I think "15 instructions" is more fair. – Eelvex – 2011-02-15T10:35:09.873

@Eelvex: Which ones didn't you count? (I'm not going to argue though :)) – None – 2011-02-15T17:15:56.863

(define (list)) and probably I counted something more? I don't know... this instructions thing is a mess ... :/ – Eelvex – 2011-02-15T17:19:38.510

2

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.

Joey

Posted 2011-02-15T00:22:45.397

Reputation: 12 260

I don't know about indexes... maybe you should only count the range operator. – Eelvex – 2011-02-15T10:40:51.053

2

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): +, |, +, =
  • 0 adverbs (0 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 B

Posted 2011-02-15T00:22:45.397

Reputation: 9 638

J wins again? :) – Eelvex – 2011-02-15T17:35:55.703

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 – 2011-02-15T17:39:45.860

1

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;
    }

Aman ZeeK Verma

Posted 2011-02-15T00:22:45.397

Reputation: 609

IDEONE: http://www.ideone.com/wZw1t

– Aman ZeeK Verma – 2011-02-15T01:16:37.343

35 instructions by my count. – Joey – 2011-02-15T20:51:41.423

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 – 2011-02-15T21:17:49.413

1

Haskell 12 Operations

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

New to Haskell. Need help to reduce it further.

fR0DDY

Posted 2011-02-15T00:22:45.397

Reputation: 4 337

Good code. Try to use a seperate function and guards instead of an if-then-else, as they don't count as instructions ;) (in my mind) – FUZxxl – 2011-02-15T12:32:18.963