Find the Serialized Integer

Task

Write a program that will take (as input) a positive integer. It will then count up from 0, appending each integer to a String, only continuing if the length of the String is less than the value of the input.

A serialized integer is defined as the fully-formed integer with the maximum value belonging to the String. By "fully-formed", the integer should have no missing digits (which would occur if the length constraint of the String is met).

The output of the program should be the serialized integer for its respective, positive input.

Rules

• It's code golf, so the shortest answer (in bytes) wins!
• Input will always be positive.
• The output must be an integer in base-10 (decimal).
• The program must be 0-indexed.

Example Input | Output

   5 | 4   (0 1 2 3 4              - Length of 5)
11 | 9   (0 1 2 3 4 5 6 7 8 9 1  - Length of 11)
12 | 10  (0 1 2 3 4 5 6 7 8 9 10 - Length of 12)
1024 | 377 (0 1 2 3 4 5 6 7 8 ...  - Length of 1024)


Note(s)

• suggested test case : 11
– Rod
Jul 11 '17 at 13:35
• @Rod Added it, hopefully it makes it easier to understand! Jul 11 '17 at 13:38
• Adding quote marks to the string in the examples might make it easier to understand that it's a string. Jul 11 '17 at 20:18
• So the first N-1 digits of the Champernowne constant, with a 0 prepended?
– user45941
Nov 14 '17 at 18:19

JavaScript (ES6), 40 37 bytes

f=(n,i=s='0')=>(s+=++i)[n]?i-1:f(n,i)
<input type=number min=1 value=1 oninput=o.textContent=f(this.value)><pre id=o>0

Edit: Saved 3 bytes with some help from @Arnauld.

05AB1E, 10 7 bytes

Idea to use prefixes from Jonathan's Jelly answer

LηJ€g›O


Try it online!

Explanation

L         # range [1 ... input]
η        # prefixes
J       # join each to string
€g     # get length of each string
›    # input is greater than string length
O   # sum


Python 2, 55 bytes

Recursive lambda port from @officialaimm's answer.

f=lambda k,s='',i=1:k>len(s+i)and f(k,s+i,i+1)or~-i


Try it online!

Japt, 13 bytes

1n@P±X l >U}a


Test it online!

Explanation

1n@ P± X l >U}a
1nX{P+=X l >U}a
Implicit: U = input integer, P = empty string
X{         }a    Return the first integer X in [0, 1, 2, ...] that returns a truthy value:
P+=X             Append X to P.
l >U        Return P.length > U.
This returns the first integer that can't fit into the U-char string.
1n                 Subtract 1 from the result.
Implicit: output result of last expression


Jelly,  11 10 9  5 bytes

ẈÄ<⁸S


A monadic link taking a positive integer and returning a non-negative integer.

Try it online!

How?

ẈÄ<⁸S - Link: positive integer, n
Ẉ     - length of (implicit decimal digits) of each of (implicit [1..n])
Ä    - cumulative sums
⁸  - n
<   - (cumulative sum) less than (n)?
S - sum


PHP, 56 bytes

for(;$argn>$l=strlen($r);)$r.=+$i++;echo$i-1-($argn<$l);


Try it online!

Haskell, 5553 50 bytes

(n#x)a|l<-a++show x=last$x-1:[n#l$x+1|length l<=n]


Try it online!

Usage is (1024#"") 0

Python 2, 60 59 58 bytes

• Thanks @Felipe for 2 bytes
i,j,k='',1,input()
while len(i+j)<k:i+=j;j+=1
print~-j


Try it online!

Pyth, 8 7 bytes

tf<Q=+d

• I love it! Using slicing as a comparison is brilliant. Jul 11 '17 at 20:20
• @isaacg It's one of the nice golf features of Pyth(on). I got the idea when I saw Neil's answer (indexing instead of slicing, but same idea). < num seq was also very helpful. Jul 11 '17 at 20:25

Perl 6, 36 bytes

{(0...^{([~] 0..$^a).comb>$_})[*-1]}


Try it online!

• 0 ...^ {...} is the sequence of numbers from zero until one less than the number for which the code block in braces returns true. (... without the caret would return the first number for which the block returned true.)
• [~] 0 .. $^a is the concatenation of numbers from 0 up to the current number $^a (the parameter to the code block).
• .comb is a list of all of the characters (digits) in the concatenated string. Interpreted as a number, it evaluates to the length of the string. .chars would be more natural to use here, since it evaluates directly to the length of the string, but the name is one character longer.
• $_ is the argument to the top-level function. • [*-1] selects the last element of the generated list. QBIC, 34 bytes {A=!p$~_lB+A|>:|_xp-1|\B=B+A]p=p+1


Explanation

{           DO infinitely
A=!p$Set A$ to p cast to num
Note that p starts out as 0.
~      >:   IF the input number is exceeded by
_l   |     the length of
B+A      A$and B$ combined
_xp-1|      THEN QUIT, printing the last int successfully added to B$The _X operator quits, (possibly) printing something if followed by a-zA-Z _x is slightly different, it prints the expression between the operator _x and | \B=B+A ELSE add A$ to B$] END IF p=p+1 Raise p, and rerun  Python 2, 44 bytes f=lambda k,i=0:-2*(k<0)or-~f(k-len(i),i+1)  Try it online! J, 26 bytes (>i:1:)([:+/\[:>.10^.1+i.) ((>i:1:)([:+/\[:>.10^.1+i.))"0 ] 5 11 12 1024 2000 20000 100000 1000000 4 9 10 377 702 5276 22221 185184  R, 43 bytes n=scan();sum(cumsum(floor(log10(1:n))+1)<n)  Try it online! Husk, 8 bytes #ȯ<¹ṁLŀḣ  Try it online! About as hard as I could condense it. Pyth is really hard to beat here. • Hard to beat, but at least possible to match... May 9 '21 at 20:53 Husk, 7 bytes #<¹∫mLḣ  Try it online! How?  ḣ # range from 1..input; mL # get the lengths of string representations of each element; ∫ # get the cumulative sums; # # how many of these <¹ # are less than the input?  Vyxals, 5 bytes ʁvL¦>  Try it Online! ʁ # 0...n vL # Get length of each ¦ # Cumulative sums > # (input) is greater than each? # (s flag) sum ToS at end  K (ngn/k), 13 bytes {(+\#'$!x)'x}


Try it online!

Takes input n as x.

• (...)'x run a binary search (returning the index of the largest value equal to or smaller than x) on the list generated by...
• (+\#'$!) • $!x stringify each value from 0..x-1
• #' count the number of characters in each stringified number
• +\ take the cumulative sums

WendyScript, 42 bytes

<<f=>(x){<<n=""#i:0->x{n+=i?n.size>=x/>i}}

f(1024) // returns 377


Try it online!

Ungolfed:

let f => (x) {
let n = ""
for i : 0->x {
n+=i
if n.size >= x
ret i
}
ret
}


PHP, 41 bytes

while(strlen($s.=+$i++)<=$argn);echo$i-2;


Java 8, 64 bytes

n->{int i=0;for(String t="0";;t+=++i)if(t.length()>n)return~-i;}


Or slight alternatives with the same byte-count:

n->{int i=0;for(String t="";;t+=i++)if(t.length()>n)return i-2;}
n->{int i=-1;for(String t="";;t+=++i)if(t.length()>n)return~-i;}


Explanation:

Try it here.

n->{                  // Method with integer as both parameter and return-type
int i=0;            //  Integer i, starting at 0
for(String t="0";   //  String, starting at "0"
;               //  Loop indefinitely
t+=++i)        //    After every iteration: append the String with i+1
//    by first increasing i by 1 with ++i
if(t.length()>n)  //   If the length of the String is larger than the input:
return~-i;      //    Return i-1
//  End of loop (implicit / single-line body)
}                     // End of method


->n{~-(0..n).find{|x|0>n-="#{x}"=~/$/}}  Try it online! Ruby, 44 bytes Inspired by Kevin Cruijssen's JAVA answer. -4 bytes thanks to G B. ->n{i,t=0,'';t+="#{i+=1}"while t.size<n;i-1}  • (i+=1;t+=i.to_s) is the same as t+="#{i+=1}", only 4 bytes longer – G B Nov 14 '17 at 7:11 • And if you do that, you don't need the variable t anymore, you can subtract the size from n and then compare with 0. – G B Nov 14 '17 at 7:14 Perl 5, 31 + 1 (-p) = 32 bytes $".=++$\while$_>=length$"}{$\--


Try it online!