Oh oh, I see!! So, you get the value of $_[0] using shift, then you split the incoming string into individual characters, then you iterate through each character one by one performing this Chinese transform on each digit:
$n = (0 . $n =~ tr/1-9/246802468/r | $n =~ tr/1-9/00001/r . $_) =~
s/^0+(?=.)//r
I have no idea what I'm looking at, but I think, if I can ever come up with code like this, I will have become a Perl hacker. Haha for example, I don't understand how transforming digits from 1-9 into 246802468 is going to end up as a decimal in the end. So, I don't understand any of this. But I find it fascinating/amazing.
I'm Harangzsolt33, but somehow I got logged out. | [reply] |
The first tr/// converts each digit to the least significant digit you get when you multiply that digit by 2.
The second tr/// produces the carry bit for each digit product.
The bitwise 'or' adds the carry to the next higher digit (because of the concatenation of the new bit).
It's sort of like how you were doing the multiply by 2, but with a better handling of the carry. Because we are only multiplying
by 2 there never will be a chained carry (where a carry causes another carry, similar to 4999+1).
| [reply] |
| [reply] |