3
\$\begingroup\$

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.

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

Mathematica

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

k = FromDigits@Reverse@IntegerDigits[#] &; 
Count[NestWhileList[# + k@# &, #, # != k@# &], _?OddQ] &
| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

Python - 10 operations

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

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
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The way you counted seems fair to me. \$\endgroup\$ – Eelvex Feb 15 '11 at 10:36
2
\$\begingroup\$

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))))))
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think "15 instructions" is more fair. \$\endgroup\$ – Eelvex Feb 15 '11 at 10:35
  • \$\begingroup\$ @Eelvex: Which ones didn't you count? (I'm not going to argue though :)) \$\endgroup\$ – user475 Feb 15 '11 at 17:15
  • \$\begingroup\$ (define (list)) and probably I counted something more? I don't know... this instructions thing is a mess ... :/ \$\endgroup\$ – Eelvex Feb 15 '11 at 17:19
2
\$\begingroup\$

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.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I don't know about indexes... maybe you should only count the range operator. \$\endgroup\$ – Eelvex Feb 15 '11 at 10:40
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ J wins again? :) \$\endgroup\$ – Eelvex Feb 15 '11 at 17:35
  • \$\begingroup\$ 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. \$\endgroup\$ – J B Feb 15 '11 at 17:39
1
\$\begingroup\$

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;
    }
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ IDEONE: ideone.com/wZw1t \$\endgroup\$ – Aman ZeeK Verma Feb 15 '11 at 1:16
  • \$\begingroup\$ 35 instructions by my count. \$\endgroup\$ – Joey Feb 15 '11 at 20:51
  • \$\begingroup\$ 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(). \$\endgroup\$ – Aman ZeeK Verma Feb 15 '11 at 21:17
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – FUZxxl Feb 15 '11 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.