5
\$\begingroup\$

This question already has an answer here:

Inspired by this question.

Champernowne's constant is an infinite decimal number that consists of "0." followed by all natural numbers concatenated together. It begins like so: 0.123456781011121314151617181920212223242526272829303132333435363738394041424344454647484950, etc. The digits of Chamernowne's constant are sequence A033307 in the OEIS.

Your Task:

Write a program or function that, when given an integer as input, outputs/returns Champernowne's constant up to the first occurance of the input. For example, if the input is 13, you should output 0.12345678910111213. If you wish, for inputs greater than 0, you may drop the leading "0." in your output.

Input:

An integer, or a string containing only numbers.

Output:

Champernowne's constant up to the first occurance of the input.

Test Cases:

0   -> 0
1   -> 0.1 OR 1
14  -> 0.1234567891011121314 OR 1234567891011121314
91  -> 0.1234567891 OR 1234567891
12  -> 0.12 OR 12
101 -> 0.123456789101 OR 123456789101 

Scoring:

This is , lowest score in bytes wins!

\$\endgroup\$

marked as duplicate by FryAmTheEggman code-golf Aug 4 '17 at 0:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ Very related. \$\endgroup\$ – ETHproductions Aug 3 '17 at 23:57
  • \$\begingroup\$ @ETHproductions, thanks, I hadn't seen that. It is very closely related, but I don't think it is quite close enough to qualify as a dupe. However, if you disagree, feel free to CV. \$\endgroup\$ – Gryphon Aug 3 '17 at 23:58
  • 1
    \$\begingroup\$ @ETHproductions I can't believe I'm getting to the point where I'm forgetting about questions that I actually answered. Anyway to me that seems like a dupe, so I'll close this for now. \$\endgroup\$ – FryAmTheEggman Aug 4 '17 at 0:01
2
\$\begingroup\$

05AB1E, 6 bytes

Code

LJ¹¡н«

Uses the 05AB1E encoding. Try it online!

Explanation

L         # Create the range [1, 2, ..., input]
 J        # Join into a single string
  ¹¡      # Split at occurences of the input
    н     # Take the first element
     «    # Append the input to the first element and implicitly output
\$\endgroup\$
  • 2
    \$\begingroup\$ And I thought this would actually be somewhat difficult :( \$\endgroup\$ – Gryphon Aug 3 '17 at 23:55
2
\$\begingroup\$

Python 3, 70 63 61 bytes

A bunch of bytes removed thanks to @Rod, and some more thanks to @WheatWizard

lambda x:''.join(map(str,range(1,x))).split(str(x))[0]+str(x)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ you can save 13 bytes using map instead list comprehension, and backticks instead str \$\endgroup\$ – Rod Aug 4 '17 at 0:31
  • \$\begingroup\$ @Rod Thanks! I've used your map suggestion \$\endgroup\$ – Luis Mendo Aug 4 '17 at 14:48
  • \$\begingroup\$ @WheatWizard Good idea, thanks! \$\endgroup\$ – Luis Mendo Aug 4 '17 at 14:53
1
\$\begingroup\$

Haskell, 82 73 55 bytes

x!b|or$zipWith(==)x b=x
x!(a:b)=a:x!b
(!(show=<<[1..]))

Try it online!

Explanation

First we define !. x!b truncates b to the first appearance of x. It does this by checking if b starts with x (or$zipWith(==)x b) returning x if it does and moving one down the string otherwise. Then we define our main function. Our main function is a point-free function that takes the constant (show=<<[1..]) and truncates it to the first appearance of x. This takes x as a string.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 111 bytes

(m=Min@SequencePosition[s=Flatten[IntegerDigits/@Range[0,#]],t=IntegerDigits@#];FromDigits@Join[s[[;;m-1]],t])&
\$\endgroup\$
1
\$\begingroup\$

Pyth, 11 8 bytes

+hcjkSQ`

Try it online! or run a Test Suite.

Simply creates the constant (jkSQ) and then prints the constant up to the input (c ... `) followed by the input (+ ...).

\$\endgroup\$
1
\$\begingroup\$

MATL, 20 15 14 bytes

U:VXzG&Yb1X)Gh

Takes the input as a string. Try it online!

Explanation

U      % Implicitly input a string. Convert to number
:      % Range from 1 to that
V      % Convert to string. Includes spaces as separators
Xz     % Remove spaces
G      % Push input again
&Yb    % Split at that. Gives a cell array
1X)    % Get contents of the first cell
G      % Push input again
h      % Concatenate horizontally. Implicitly display
\$\endgroup\$

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