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.
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.480if 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