25
\$\begingroup\$

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.

N(削除) the 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.

asked Feb 23, 2016 at 2:49
\$\endgroup\$
0

12 Answers 12

15
\$\begingroup\$

JavaScript (ES6), 31 bytes

n=>~-(n*9+4).toPrecision(1)/9|0

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:

n=>((n*9+4).toPrecision(1)-1)/9>>>0

35 bytes, works up to 16666666667.

n=>((n=(n*9+4).toPrecision(1))-n[0])/9

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

answered Feb 23, 2016 at 8:49
\$\endgroup\$
7
  • \$\begingroup\$ @user81655 I've added some alternative versions with their numeric limitations. \$\endgroup\$ Commented Feb 23, 2016 at 11:44
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 23, 2016 at 11:54
  • \$\begingroup\$ @user81655 I lowered the upper bound to allow the solution. \$\endgroup\$ Commented Feb 23, 2016 at 19:17
  • \$\begingroup\$ Got you to 10k @Neil, love the golfs! \$\endgroup\$ Commented Jun 8, 2016 at 2:39
  • 1
    \$\begingroup\$ @NiCkNewman Woohoo! Thanks! \$\endgroup\$ Commented Jun 8, 2016 at 7:36
5
\$\begingroup\$

Jelly, 16 bytes

ḤRμDIASμÐḟμạ3ỤḢị

Try it online!

How it works

ḤRμDIASμÐḟμạ3ỤḢị 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)
 ạ3 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.
answered Feb 23, 2016 at 5:09
\$\endgroup\$
4
\$\begingroup\$

Oracle SQL 11.2, 200 bytes

WITH v(i)AS(SELECT 0 FROM DUAL UNION ALL SELECT DECODE(SIGN(i),0,-1,-1,-i,-i-1)FROM v WHERE LENGTH(REGEXP_REPLACE(:1+i,'([0-9])1円+','1円'))>1)SELECT:1+MIN(i)KEEP(DENSE_RANK LAST ORDER BY rownum)FROM v;

Un-golfed

WITH v(i) AS
(
 SELECT 0 FROM DUAL -- Starts with 0
 UNION ALL
 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
)
SELECT :1+MIN(i)KEEP(DENSE_RANK LAST ORDER BY rownum) FROM v;
answered Feb 23, 2016 at 8:12
\$\endgroup\$
3
\$\begingroup\$

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.

hSh.g.a-kQsmsM*RdjkUTtBl`Q

Test Suite.

answered Feb 23, 2016 at 3:02
\$\endgroup\$
0
3
\$\begingroup\$

Pyth, 16 bytes

haDQsM*M*`MTSl`Q

Try it online: Demonstration or Test Suite

Explanation:

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
answered Feb 23, 2016 at 10:43
\$\endgroup\$
3
\$\begingroup\$

MATL, 25 bytes

2*:"@Vt!=?@]]N$vtG-|4#X<)

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
answered Feb 23, 2016 at 10:32
\$\endgroup\$
3
+500
\$\begingroup\$

Perl, 32

Based on the beautiful JavaScript solution by Neil.

$_=0|1/9*~-sprintf"%.e",$_*9+4.1

Starts to fail at 5e15

answered Feb 23, 2016 at 16:50
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 122 bytes

f@x_:=Last@Sort[Flatten@Table[y*z,{y,1,9},{z,{FromDigits@Table[1,10~Log~x+1-Log[10,1055555]~Mod~1]}}],Abs[x-#]>Abs[x-#2]&]

Function named x.

answered Feb 23, 2016 at 4:17
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 59 bytes

n=>eval(`for(i=a=0;i<=n;a=i%10?a:++i)p=i,i+=a;n-p>i-n?i:p`)

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.

f=(n,p,a,i=0)=>n<i?n-p>i-n?i:p:f(n,i,(i-=~a)%10?a:i++,i)

Explanation

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 =
n=>
 eval(` // eval enables for loop without {} or return
 for(
 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>

answered Feb 23, 2016 at 8:14
\$\endgroup\$
1
  • \$\begingroup\$ I lowered the upper bound to allow your solution. \$\endgroup\$ Commented Feb 23, 2016 at 19:17
2
\$\begingroup\$

Japt, 18 bytes

9*U+4 rApUs l1/9|0

Try it online!

Based on Neil's technique

Non-competing solution:

*9+4 h /9|0
answered Jan 12, 2017 at 20:52
\$\endgroup\$
2
  • 1
    \$\begingroup\$ And now you can do *9+4 h /9|0 :-) \$\endgroup\$ Commented Jan 13, 2017 at 0:12
  • \$\begingroup\$ @ETHproductions Thanks! I'm having a lot of fun with Japt :) \$\endgroup\$ Commented Jan 13, 2017 at 0:22
1
\$\begingroup\$

05AB1E, 20 bytes

×ばつ}) ̃ïD1-ÄWQÏ{¬

Try it online!

9Ý # Push 0..9
 ×ばつ}) ̃ # For each digit, 0-9, push 1-7 copies of that number.
 ïD # Convert to integers, dupe the list.
 1 # Push original input (n).
 -Ä # Push absolute differences.
 WQ # Get min, push 1 for min indices.
 Ï{¬ # Push indices from original array that are the min, sort, take first.
answered Jan 12, 2017 at 16:01
\$\endgroup\$
2
  • \$\begingroup\$ 99 is surely more lazy than 111, as it only requires two button presses. \$\endgroup\$ Commented Jan 12, 2017 at 16:02
  • \$\begingroup\$ @Adám fair enough, added head command. \$\endgroup\$ Commented Jan 12, 2017 at 16:04
1
\$\begingroup\$

Mathematica, 56 bytes

Min@Nearest[##&@@@Table[d(10^n-1)/9,{n,0,6},{d,0,9}],#]&

Pure function with first argument #, works for inputs up to 10^6.

For a nonnegative integer n and a digit d, 10^n-1 = 99...9 (9 repeated n times), so d(10^n-1)/9 = dd...d (d repeated n times). Creates a Table of values for 0 <= n <= 6 and 0 <= d <= 9, then flattens the table, finds the list of elements Nearest to # and takes the Min.

I believe this version will work for arbitrarily large integers:

Min@Nearest[##&@@@Table[d(10^n-1)/9,{n,0,IntegerLength@#},{d,0,9}],#]&
answered Jan 13, 2017 at 1:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.