[Question]
  [
When I was a kid, and wanted to count the dollar bills in my life savings, I would count out loud:
> 
> one, two, three, four, five, six, seven, eight, nine, ten;  
> 
>  eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty;  
> 
>  twenty-one, twenty-two, twenty-three, twenty-four, twenty-five...
> 
> 
> 
Eventually I got tired of pronouncing each of these multi-syllable numbers. Being mathematically minded, I created a much more efficient method of counting:
> 
> one, two, three, four, five, six, seven, eight, nine, ten;  
> 
>  one, two, three, four, five, six, seven, eight, nine, twenty;  
> 
>  one, two, three, four, five, six, seven, eight, nine, thirty...
> 
> 
> 
As you can see, I would only pronounce the digit(s) that have changed from the previous number. This has the added advantage that it's considerably more repetitive than the English names for numbers, and therefore requires less brainpower to compute.
## Challenge
Write a program/function which takes in a positive integer and outputs/returns how I would count it: that is, the right-most non-zero digit and all trailing zeroes.
### Examples
```
   1    1
   2    2
  10   10
  11    1
  29    9
  30   30
  99    9
 100  100
 119    9
 120   20
 200  200
 409    9
1020   20
```
A full list of test-cases shouldn't be necessary. This is [A274206](http://oeis.org/A274206) on OEIS.
## Rules
* Your entry must theoretically work for all positive integers, ignoring precision and memory issues.
* Input and output must be in decimal.
* You may choose to take input and/or output as a number, a string, or an array of digits.
* Input is guaranteed to be a positive integer. Your entry can do anything for invalid input.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so **the shortest code in bytes wins.**
  
[Answer]
# [Python 2](https://docs.python.org/2/), 28 bytes
```
f=lambda n:n%10or 10*f(n/10)
```
[Try it online!](https://tio.run/nexus/python2#DcexCoAgEADQub7iltDioDubFPqSajBCEOoKcfDvzenxalhv/5yXB3EyML0JmKagZWYaa2gtEAU2RoNMyE2LC6G1re2G0BAdru@@FCVDQbVnhRB0GesP "Python 2 – TIO Nexus")
A recursive formula works out very cleanly. If the last digit is nonzero, output it. Otherwise, remove the final zero, compute the output for that, and multiply it by 10.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~6~~ 3 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-3 bytes by having I/O as a [decimal list of digits](https://codegolf.stackexchange.com/questions/110836/efficient-counting/110861#comment269927_110836).
```
ṫTṪ
```
Test suite at **[Try it online!](https://tio.run/nexus/jelly#@/9w5@qQhztX/dc53O7Ndbj9UdOayP//o6MNY3WijYDYUMcATIL5OpZA0hgsYglmA2Vh8lC@EZhvBBU3AdIwdSCZWAA)**
### How?
```
ṫTṪ - Main link: listOfDigits  e.g.  [1,    0,    2,    0]  or [1,      1,    9  ]
 T  - truthy indexes                 [1,          3      ]     [1,      2,    3  ]
ṫ   - tail (vectorises)              [[1,0,2,0],  [2,0]  ]     [[1,1,9],[1,9],[9]]
  Ṫ - tail pop                                    [2,0]                       [9]
```
---
If we could not take decimal lists a 6 byter is:
```
DµṫTṪḌ
```
Which you can see **[here](https://tio.run/nexus/jelly#@@9yaOvDnatDHu5c9XBHz/@jew63P2pa4/7/f7ShjpGOoYGOIZC21DE20LG0BHJBfCBtZKBjBGSbGIDEjAxiAQ)**.
This does the same thing, but converts an integer to a decimal list beforehand and converts back to an integer afterwards.
[Answer]
# C, ~~30~~ ~~29~~ 27 bytes
Proud of this as I abuse two C exploits to golf this up (described at end of post); This is specifically for C (GCC)
---
3) `b=10;f(a){a=a%b?:b*f(a/b);}`//27 bytes
2) ~~`b;f(a){b=a=a%10?:10*f(a/10);}`~~ //29 bytes
1) ~~`f(i){return i%10?:10*f(i/10);}`~~ //30 bytes
[Try it online (27 byte version)](https://tio.run/nexus/c-gcc#@59ka2hgnaaRqFmdaJuommRvlaQF5OknaVrX/s/MK1HITczM09Cs5lJQKCgC8tM0lFQzY/KUdNI0jDU1rbEIm2EXNjTAIW6ESxyXBgPcMgZQmaLUktKiPAUDa67a/1/z8nWTE5MzUgE)
---
First attempt (30 bytes): Abuses the fact that in GCC if no value is declared in ternary, the conditional value will be returned. Hence why my ternary operator is blank for the truth return value.
Second attempt (29 bytes): Abuses **memory bug** in GCC where, as far as I understand, if a function has no return value, when more than two variables have been meaningfully utilized in the function, the last set value of the first argument variable will be returned.  
   *(Edit: but this "set value" must set in certain ways, for example setting a variable with `=` or `+=` works but setting it with `%=` does not work; weird)*
Third attempt (27 bytes): Since I must meaningfully utilize the second variable (b) anyways to properly abuse the memory bug mentioned above, I may as well use it as an actual variable for "10" for substitution.   
   *(Note: I should be able to swap `a=a%b` with `a%=b` to save another byte but unfortunately this causes the memory bug exploit above to stop "working", so I can't)*
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~7~~ 6 bytes
```
!`.0*$
```
[**Try it online**](https://tio.run/nexus/retina#U9VI4PqvmKBnoKXy/78hlxGXoQGXIZC25DI24LK0BHJBfCBtZMBlBGSbGIDEDA0A) (all test cases)
Output matches of a digit followed by any zeros at the end of the input string. Though not required, this also happens to work for `0`.
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 18 ~~32~~ bytes
~~I think I'll have to spend sometime on this later and see if I can compress it a bit. But for the moment here it is.~~  
Turns out I was thinking about this totally the wrong way. Now the process incrementally applies a mod (1,10,100,1000,...) to the input integer and prints out the first one which isn't zero. Bit more boring, but shorter.
```
!N%*I1^\.u;u@O\;;r
```
[Try it here](https://ethproductions.github.io/cubix/?code=IU4lKkkxXlwudTt1QE9cOzty&input=MTAyMjAxMA==&speed=20)
```
    ! N
    % *
I 1 ^ \ . u ; u
@ O \ ; ; r . .
    . .
    . .
```
[Answer]
## JavaScript, 21 bytes
```
f=n=>n%10||10*f(n/10)
```
### Test cases
```
f=n=>n%10||10*f(n/10)
console.log(f(1   )); //  1
console.log(f(2   )); //  2
console.log(f(10  )); //  10
console.log(f(29  )); //  9
console.log(f(99  )); //  9
console.log(f(1020)); //  20
```
[Answer]
## Javascript ~~19~~ 18 bytes
Thanks to ETHproductions for golfing off one byte and Patrick Roberts for golfing off two bytes
```
x=>x.match`.0*$`
```
Returns an array of strings that match the regex of being at the end of the input string with any character followed by the largest possible number of zeroes.
[Try it Online](https://tio.run/nexus/javascript-node#S7P9X2FrV6GXm1iSnKGhr2egpaKfrvm/uKTINjqWqzwjMydVA8jRy0nNSy/JsLE00ATxCkqLMzSUlLSRpLQNDTU1uZLz84rzc1L1cvLTwXK5iQUaaZp6WfmZeRrqCuqamv8B)
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 2 bytes
```
a₁
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6Ompoe7Omsfbp3wP/FRU@P//9GGOkY6hgY6hkDaUsfYQMfSEsgF8YG0kYGOEZBtYgASMzKIBQA "Brachylog – Try It Online")
The suffix builtin `a₁`, for integers, is implemented as:
```
brachylog_adfix('integer':1, 'integer':0, 'integer':0).
brachylog_adfix('integer':1, 'integer':I, 'integer':P) :-
    H #\= 0,
    H2 #\= 0,
    abs(P) #=< abs(I),
    integer_value('integer':Sign:[H|T], I),
    integer_value('integer':Sign:[H2|T2], P),
    brachylog_adfix('integer':1, [H|T], [H2|T2]).
```
Brachylog likes to be able to treat integers as lists of digits, and for that it uses the custom utility predicate `integer_value/2`. The interesting thing about `integer_value/2` here is that since it has to be able to correctly translate a digit list with leading zeros, it ends up also being able to translate an integer to a digit list with leading zeros, so predicates that don't want that to happen (most of them, *especially* the nondet ones like `a`) forbid the heads of their digit lists from being 0. So, while `a₁` generates suffixes shortest first for lists and strings, it skips over any suffix of an integer with a leading 0, which in addition to removing duplicates, also means that the first suffix generated is the rightmost non-zero digit with all trailing zeroes.
[Answer]
# Bash + coreutils, 12
```
grep -o .0*$
```
[Try it online](https://tio.run/nexus/bash#@59elFqgoJuvoGegpfL/vyGXEZehAZchkLbkMjbgsrQEckF8IG1kwGUEZJsYgMSMDAA).
[Answer]
## [Grime](https://github.com/iatorm/grime), 5 bytes
```
d\0*e
```
[Try it online!](https://tio.run/nexus/grime#@58SY6CV@v@/oYGBkQEA "Grime – TIO Nexus")
## Explanation
```
       Find the longest substring of the input matching this pattern:
d      a digit, then
 \0*   zero or more 0s, then
    e  edge of input (which is handled as an Nx1 rectangle of characters).
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 74 bytes
```
{({}<>)<>}(()){{}<>(({}<>)[((((()()()){}){}){}){}]<(())>){((<{}{}>))}{}}{}
```
[Try it online!](https://tio.run/nexus/brain-flak#PYrBCQAxEALb0Ucg3HvZRsK9rgyx9k2Wg@ggKJYgRzLSAKku@KeFFtuUL2/0MSkgZDnJk4eqZ84a3wY "Brain-Flak – TIO Nexus")
Only prints the last non-0 and all trailing 0s.
**Explanation:**
```
{({}<>)<>}                    # Move everything to the other stack (reverse the input)
(())                          # Push a 1 to get started
{                             # do...
  {}<>                        #   pop the result of the equals check (or initial 1)
  (                           #   push...
    ({}<>)                    #     the top value from the other stack (also put this on the stack)
    [((((()()()){}){}){}){}]  #     minus the ASCII value of 0
    <(())>                    #     on top of a 1
  )                           #   close the push   
  {                           #   if not zero (ie. if not equal)
    ((<{}{}>))                #     replace the 1 from 3 lines back with a 0
  }{}                         #   end if and pop the extra 0
}                             # while the copied value != "0"
{}                            # pop the result of the equals check
```
[Answer]
# Vim, 19 bytes
Two versions, both 19 bytes:
```
:s/\v.*([^0]0*)/\1
:s/.*\([^0]0*\)/\1
```
Plus a trailing carriage return on each.
[Verify all test-cases online!](https://tio.run/nexus/v#@2@lWqyvpxWjER1nEGugFaOpH2P4/78hlxGXoQGXIZC25DI24LK0BHJBfCBtZMBlBGSbGIDEjAwA) (One byte added to test on multiple lines)
[Answer]
# TI-Basic, 18 bytes
```
If fPart(.1Ans
Return
Ans.1
prgmA
10Ans
```
[Answer]
# R, 33 bytes
Implemented as an unnamed function
```
function(x)rle(x%%10^(0:99))$v[2]
```
This applies a mod of 10^0 through 10^99. `rle` is used to reduce the results down so that the second item is always the result we want.  
[Try it online!](https://tio.run/nexus/r#S7PlSivNSy7JzM/TqNAsyknVqFBVNTSI0zCwsrTU1FQpizaK/V9elFmSqlGcWFCQU6lRnFqoYaRjaGBgoGOkqZOmqaOurmNqoPn/PwA "R – TIO Nexus")
[Answer]
# [Zsh](https://www.zsh.org/), ~~18~~ 16 bytes
```
<<<${(M)1%[^0]*}
```
~~[Try it online!](https://tio.run/##qyrO@F@ekZmTqlCUmpiikJOZl2qtkJLPlVqWmKNQnFqioKuroAIS/W9jY6NSreGraagaHWcQq1X7PyUfKGrIZcRlzGXCZWjAZWwAxEYGXCYGYAwigDwA "Zsh – Try It Online")~~
[Try it online!](https://tio.run/##qyrO@F@ekZmTqlCUmpiikJOZl2qtkJLPlVqWmKNQnFqioKuroAIS/W9jY6NSreGraagaHWcQq1X7PyUfKGrIZcRlzGXCZWjAZWwAxEYGXCYGYAwigDwA "Zsh – Try It Online")
# [Bash](https://www.gnu.org/software/bash/), ~~25~~ 20 bytes
***-5 bytes** by @roblogic*
```
echo ${1#${1%[^0]*}}
```
[Try it online!](https://tio.run/##S0oszvhfnpGZk6pQlJqYopCTmZdqrZCSz5ValpijUJxaoqCrq6ACEv2fmpyRr6BSbagMxKrRcQaxWrW1/1PygTKGXEZcxlwmXIYGXMYGQGxkwGViAMYgAsgDAA "Bash – Try It Online")
~~[Try it online!](https://tio.run/##S0oszvhfnpGZk6pQlJqYopCTmZdqrZCSz5ValpijUJxaoqCrq6ACEv1fZKtSbagaHWcQq1XLlZqcka8A5CurFNX@T8kHShtyGXEZc5lwGRpwGRsAsZEBl4kBGIMIIA8A "Bash – Try It Online")~~
---
Shells need to call external programs to use regex, so we have to make do with globbing.
The `${1%[^0]*}` expansion matches the shortest suffix beginning with a nonzero character, and removes it.
* In Zsh, adding the `(M)` flag causes the matched suffix to be kept instead of removed.
* In Bash, the `${1% }` expansion removes as a prefix whatever is left.
[Answer]
## [GNU sed](https://en.wikipedia.org/wiki/Sed), ~~17~~ 14 + 1(r flag) = 15 bytes
**Edit:** 2 bytes less thanks to [Riley](/users/57100/riley)
```
s:.*([^0]):\1:
```
It works by deleting everything until the right-most nonzero digit, which is then printed along with any existing trailing zeros. The script can handle multiple tests in one run, each on a separate line.
[**Try it online!**](https://tio.run/nexus/bash#K05NUdAtUlD/X2ylp6URHWcQq2kVY2j1X/2/IZcRl6EBlyGQtuQyNuCytARyQXwgbWTAZQRkmxiAxIwMAA) (all test examples)
[Answer]
## Mathematica, 26 bytes
Pure function which takes a list of digits and outputs a list of digits:
```
#/.{___,x_,y:0...}:>{x,y}&
```
## Explanation
```
#                           First argument
 /.                           Replace
   {                              start of list followed by
    ___,                          zero or more elements followed by
        x_,                       an element (referred to later as x) followed by
           y:0...                 a sequence of zero or more 0s (referred to later as y) followed by
                 }                end of list
                  :>            with
                    {x,y}         {x,y}
                         &   End of function.
```
This works since it finds the leftmost match for `x`, which must be the rightmost nonzero element of the list since it is followed by a sequence of zero of more `0`s and then the end of the list. 
[Answer]
# Java 8, 47 bytes
this is a lambda expression assignable to a `IntUnaryOperator`:
```
x->{int m=1;for(;x%m<1;m*=10);return x%m*m/10;}
```
explanation:
multiply m by 10 until `x%m` is not 0.
`return x%m*m/10` requires the division because m is an order of magnitude more than the desired result.
[Answer]
# [Perl 6](http://perl6.org/), 10 bytes
```
{+m/.0*$/}
```
Trivial regex solution. Inputs and outputs a number.
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~10~~ 7 bytes
*3 bytes saved thanks to [@B. Mehta!](https://codegolf.stackexchange.com/users/64748/b-mehta)*
```
tfX>Jh)
```
Input and output are an array of digits.
[Try it online!](https://tio.run/nexus/matl#@1@SFmHnlaH5/3@0oY6BjpGOQSwA "MATL – TIO Nexus")
Or [verify all test cases](https://tio.run/nexus/matl#S/hfkhZh55Wh@d8l5H@0YSxXtBEQGyoYgEkwX8ESSBqDRSzBbKAsTB7KNwLzjaDiJkAaIg5kgKQA).
### Explanation
```
t     % Input string implicitly. Duplicate
f     % Push indices of non-zero digits
X>    % Keep maximum, say k
Jh    % Attach 1j to give [k, 1j]. This is interpreted as an index "k:end"
)     % Index into original string. Display implcitly
```
[Answer]
# C#, ~~30~~ 28 bytes
Based on [this JavaScript answer](https://codegolf.stackexchange.com/a/110872/65012), so I suppose all the credits kinda go to him.
## Golfed
```
i=a=>a%10<1?10*i(a/10):a%10;
```
* -2 bytes by removing `()` around `a` thanks to [Emigna](https://codegolf.stackexchange.com/users/47066/emigna)
[Answer]
# J, 27 bytes
```
10&|`(10*(p%&10))@.(0=10&|)
```
It's based off of xnor's formula, so credits to him.
[Answer]
# Kotlin, 49 bytes
lambda, assignable to `(List<Int>) -> List<Int>`
```
{a->a.slice(a.indexOfLast{it in 1..9}..a.size-1)}
```
* implicit parameter name `it` in `indexOfLast`
* `..` for building ranges
[Answer]
# Perl 5, 12 bytes
11, plus 1 for `-nE` instead of `-e`
```
say/(.0*$)/
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
RD0Ê1k>£R
```
[Try it online!](https://tio.run/nexus/05ab1e#@x/kYnC4yzDb7tDioP//ow11DHSMdAxiAQ)
or as a 
[Test suite](https://tio.run/nexus/05ab1e#qymrDP4f5GJwuMsw2@7Q4qD/lUqH91spHN6vpPPfkMuIy9CAyxBIW3IZG3BZWgK5ID6QNjLgMgKyTQxAYkYGAA)
**Explanation**
```
R          # reverse input
 D         # duplicate
  0Ê       # check each for inequality with 0
    1k     # get the index of the first 1
      >    # increment
       £   # take that many digits from the input
        R  # reverse
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 6 bytes
```
f".0*$
```
[Try it online!](https://tio.run/##y0osKPn/P01Jz0BL5f9/JUMDIwMlAA "Japt – Try It Online")
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 5 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
æΩ$3╚
```
[Run and debug it](https://staxlang.xyz/#p=91ea2433c8&i=1%0A2%0A10%0A11%0A30%0A99%0A100%0A409%0A1020%0A&a=1&m=2)
### Procedure:
1. Calculate "multiplicity" by 10. (That's the number of times 10 evenly divides the input)
2. Add 1.
3. Keep that many characters from the right of (the input as a string).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 4 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ĀÅ¡θ
```
I/O as a list of digits.
[Try it online](https://tio.run/##yy9OTMpM/f//SMPh1kMLz@34/z/aUsdAxxiITYDYIBYA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9TVmmvxPmobRKnkn2lS/D/Iw2HWw8tPLfjv5fOf0MuIy5DAy5DIG3JZWzAZWkJ5IL4QNrIgMsIyDYxAIkBOZYGxgYmBgYA) (test suite contains a join for better readability).
**Explanation:**
```
Ā     # Python-style truthify each digit in the (implicit) input-list (0 if 0; 1 if [1-9])
      #  i.e. [9,0,4,0,3,0,0] → [1,0,1,0,1,0,0]
 Å¡   # Split the (implicit) input-list on truthy values (1s)
      #  i.e. [9,0,4,0,3,0,0] and [1,0,1,0,1,0,0] → [[],[9,0],[4,0],[3,0,0]]
   θ  # And only leave the last inner list
      #  i.e. [[],[9,0],[4,0],[3,0,0]] → [3,0,0]
      # (after which it is output implicitly as result)
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2) `t`, 3 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
V€ỵ
```
[Try it online!](https://Not-Thonnu.github.io/run#aGVhZGVyPSZjb2RlPVYlRTIlODIlQUMlRTElQkIlQjUmZm9vdGVyPSZpbnB1dD0xJTJDMCUyQzIlMkMwJmZsYWdzPXQ=)
Port of Jonathan Allan's Jelly answer.
#### Explanation
```
V€ỵ  # Implicit input
V    # Truthy indices
 €ỵ  # Head remove over each
     # Take the last item
     # Implicit output
```
[Answer]
# [Desmos](https://www.desmos.com/calculator), 62 bytes
Input (variable `A`) and output (variable `B`) are both arrays of digits. Run the ticker to start the program.
**Ticker:**
```
B->join(A[n],B),n->\left\{A[n]=0:n-1,0\right\}
```
**Expression List:**
```
B=[]
n=A.length
```
[Try it on Desmos!](https://www.desmos.com/calculator/yberrez2td)
Scans `A` from right to left until a nonzero digit is encountered. Afterwards, set `n=0` to invalidate the action and halt the program.
]