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.


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.


   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 on OEIS.


  • 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 , so the shortest code in bytes wins.

  • \$\begingroup\$ So does "in decimal" include a list of decimal digits, like [1,0,2,0] -> [2,0] for the last test case? (I'm unclear on the phrase "single-item array"). \$\endgroup\$ – Jonathan Allan Feb 21 '17 at 23:26
  • 1
    \$\begingroup\$ @JonathanAllan By "single-item array" I meant an array that contains a single number or string which represents the integer. I didn't think allowing arrays of digits was a good idea, but now it kind of seems like an arbitrary restriction since strings are allowed (and strings are very similar to arrays in many languages). So I'll allow an array of digits unless there's a good reason not to. \$\endgroup\$ – ETHproductions Feb 21 '17 at 23:38
  • 5
    \$\begingroup\$ Dang it, you stole my secret :P \$\endgroup\$ – LegionMammal978 Feb 22 '17 at 2:07
  • 1
    \$\begingroup\$ I think almost everyone counted like this as a kid. ;) At least I did as well. :) \$\endgroup\$ – Kevin Cruijssen Feb 22 '17 at 8:35
  • 7
    \$\begingroup\$ @KevinCruijssen "as a kid"? \$\endgroup\$ – Martin Ender Feb 22 '17 at 8:55

34 Answers 34


Batch, 88 bytes

@set z=
@if %s:~-1%==0 set z=0%z%&set s=%s:~,-1%&goto l
@echo %s:~-1%%z%

Takes input on STDIN.


Ruby, 14 bytes


Input is a string (allowed by the rules)


Haskell, 38 bytes

x!d|m<-mod x d,m>0=m|e<-10*d=x!e

Try it online! Usage: (!10) 1020 yields 20.


x!d is a recursive formula which computes x modulo d with initially d=10. If the result is larger zero it is returned, otherwise recursively x!(10*d) is called.

Other approaches: (39 bytes)

f x=[y|y<-[x`mod`10^e|e<-[1..]],y>0]!!0
f x=[y|y<-map(mod x.(10^))[1..],y>0]!!0

Java 8, 33 bytes


I/O as a String.

Try it online.


s->                         // Method with String as both parameter and return-type
  s.replaceAll(".*?(.0*)$", //  Replace all occurrences of this match,
               "$1")        //  with this replacement

Regex explanation:

.*?(.0*)$                   // MATCH:
.*?                         //  Zero or more optional characters
                            //  (the optional is used to give other 0+ matches 
                            //   higher priority/precedence)
    .                       //  Followed by any character
     0*                     //  Followed by zero or more literal 0s
        $                   //  At the end of the string
   (   )                    //  And capture those non-optional characters in capture group 1

$1                          // REPLACEMENT:
                            //  The content of capture group 1, which basically removes
                            //  the optional characters outside the capture group

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.