Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Related: Program my microwave oven. Inspired by Generate lazy microwave input.

The lazy value of the non-negative integer N is the smallest of the integers that are closest to N while all their digits are identical.

Return (by any means) the lazy value of a given (by any means) N.

Nthe largest integer that your language represents in non-exponent form by default. 1000000 (A lot of interesting solutions are lost because of this too-high requirement.)

Test cases:

   0 →    0
   8 →    8
   9 →    9
  10 →    9
  16 →   11
  17 →   22
  27 →   22
  28 →   33
 100 →   99
 105 →   99
 106 →  111
 610 →  555
 611 →  666
7221 → 6666
7222 → 7777 

The colleague in question proved that there will be no ties: Except for 9/11, 99/111, etc. for which one is shorter than the other, two consecutive valid answers are always an odd distance apart, so no integer can be exactly equidistant from them.

share|improve this question

JavaScript (ES6), 31 bytes


Directly computes the lazy value for each n.

Edit: Only works up to 277777778 due to the limitations of JavaScript's integer type. Alternative versions:


35 bytes, works up to 16666666667.


38 bytes, works up to 944444444444443. But that's still some way short of 253 which is 9007199254740992.

share|improve this answer
@user81655 I've added some alternative versions with their numeric limitations. – Neil Feb 23 at 11:44
I couldn't get this algorithm to work with Number.MAX_SAFE_INTEGER either because 8e16 - 1 is expressed as 8e16. Sadly, it looks like the only way would be hard-coding the maximum result. +1 nonetheless. – user81655 Feb 23 at 11:54
@user81655 I lowered the upper bound to allow the solution. – Adám Feb 23 at 19:17
Got you to 10k @Neil, love the golfs! – NiCk Newman Jun 8 at 2:39
@NiCkNewman Woohoo! Thanks! – Neil Jun 8 at 7:36

Jelly, 16 bytes


Try it online!

How it works

ḤRµDIASµÐḟµạ³ỤḢị  Main link. Input: n

Ḥ                 Compute 2n.
 R                Yield [1, ..., 2n] or [0].
  µ               Begin a new, monadic chain. Argument: R (range)
   D              Convert to base 10.
    I             Compute all differences of consecutive decimal digits.
     A            Take the absolute values of the differences.
      S           Sum the absolute values.
       µÐḟ        Filter-false by the chain to the left.
          µ       Begin a new, monadic chain. Argument: L (lazy integers)
           ạ³     Take the absolute difference of each lazy integer and n (input).
             Ụ    Grade up; sort the indices of L by the absolute differences.
                  This is stable, so ties are broken by earlier occurrence and,
                  therefore, lower value.
              Ḣ   Head; retrieve the first index, corresponding to the lowest
                  absolute difference.
               ị  Retrieve the item of L at that index.
share|improve this answer

Pyth - 26 bytes

This answer doesn't always return the smallest value in a tie, but that isn't in the specs, so awaiting clarification fixed for 3 bytes.


Test Suite.

share|improve this answer

Oracle SQL 11.2, 200 bytes



WITH v(i) AS
  SELECT 0 FROM DUAL      -- Starts with 0
  SELECT DECODE(SIGN(i),0,-1,-1,-i,-i-1) -- Increments i, alternating between negatives and positives
  FROM   v 
  WHERE  LENGTH(REGEXP_REPLACE(:1+i,'([0-9])\1+','\1'))>1  -- Stop when the numbers is composed of only one digit
share|improve this answer

Pyth, 16 bytes


Try it online: Demonstration or Test Suite


haDQsM*M*`MTSl`Q   implicit: Q = input number
              `Q   convert Q to a string
             l     take the length
            S      create the list [1, 2, ..., len(str(Q))]
         `MT       create the list ["0", "1", "2", "3", ..., "9"]
        *          create every combination of these two lists:
                   [[1, "0"], [1, "1"], [1, "2"], ..., [len(str(Q)), "9"]]
      *M           repeat the second char of each pair according to the number:
                   ["0", "1", "2", ..., "9...9"]
    sM             convert each string to a number [0, 1, 2, ..., 9...9]
  D                order these numbers by:
 a Q                  their absolute difference with Q
h                  print the first one
share|improve this answer

MATL, 25 bytes


Uses brute force, so it may take a while for large numbers.

Try it online!

2*:       % range [1,2,...,2*N], where is input
"         % for each number in that range
  @V      %   push that number, convert to string
  t!=     %   test all pair-wise combinations of digits for equality
  ?       %   if they are all equal
    @     %     push number: it's a valid candidate
  ]       %   end if
]         % end for each
N$v       % column array of all stack contents, that is, all candidate numbers
t         % duplicate
G-|       % absolute difference of each candidate with respect to input
4#X<      % arg min
)         % index into candidate array to obtain the minimizer. Implicitly display
share|improve this answer

Mathematica, 122 bytes


Function named x.

share|improve this answer

JavaScript (ES6), 59 bytes


Recursive Solution (56 bytes)

This is a bit shorter but does not work for n > 1111111110 because the maximum call stack size is exceeded, so it is technically invalid.



Iterates through every lazy number until it gets to the first which is greater than n, then compares n to this and the previous number to determine the result.

var solution =

  eval(`           // eval enables for loop without {} or return
      i=a=0;       // initialise i and a to 0
      i<=n;        // loop until i > n, '<=' saves having to declare p above
      a=i%10?a:++i // a = amount to increment i each iteration, if i % 10 == 0 (eg.
    )              //     99 + 11 = 110), increment i and set a to i (both become 111)
      p=i,         // set p before incrementing i
      i+=a;        // add the increment amount to i
    n-p>i-n?i:p    // return the closer value of i or p
N = <input type="number" oninput="R.textContent=solution(+this.value)"><pre id="R"></pre>

share|improve this answer
I lowered the upper bound to allow your solution. – Adám Feb 23 at 19:17

Perl, 32

Based on the beautiful JavaScript solution by Neil.


Starts to fail at 5e15

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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