8
\$\begingroup\$

The Champernowne Constant is the irrational number 0.1234567891011... extending ad infinum.

We've done a question about it before. But this question is about its reciprocal, known as the Inverse Champernowne Constant. This is approximately 8.10000007.

Given a string of between one and three digits (every 3-digit string appears within the first 10,000 decimal places) inclusive, give the number of decimal places in the decimal portion of the Inverse Champernowne Constant that precede the first appearance of that string.

Builtins that perform this directly or generate approximations of either of these constants are forbidden, but builtins for arbitrary precision arithmetic are still permissible.

This is code golf, so the shortest program, in bytes, wins.

Test cases:

1 0
2 52
3 13
4 29
5 36
6 7
7 8
8 27
9 23
10 0
100 0
998 187
999 67
01 321
001 689
010 418
\$\endgroup\$
  • 1
    \$\begingroup\$ Could you post a file the first 10,000 inverse Champernowne constant digits? \$\endgroup\$ – orlp Feb 10 '16 at 1:59
  • \$\begingroup\$ @orlp gist.github.com/SuperJedi224/9c9adad78d872e4d568f to 10,000 decimal places (your program should only look at the part to the right of the decimal point) \$\endgroup\$ – SuperJedi224 Feb 10 '16 at 3:29
  • \$\begingroup\$ Can result be 1-based? \$\endgroup\$ – Luis Mendo Feb 10 '16 at 10:21
  • \$\begingroup\$ @LuisMendo The specs currently dictate 0-based \$\endgroup\$ – SuperJedi224 Feb 10 '16 at 11:10
3
\$\begingroup\$

MATL, 37 bytes

Thanks to @AndrasDeak for his help with Octsympy's vpa function!

'1/'1e4:"@V]N$h1e4H$Y$t32>)I0h)jXf1)q

EDIT (June 11, 2016): due to changes in the language, replace 0 by J in the code. The link below includes that modification

Try it online!

It takes a while in the online compiler (less than 1 minute).

'1/'        % literal string
1e4:        % array [1,2,...,1e4]
"           % for each number in that array
  @V        % push number and convert to string
]           % end loop
N$h         % concatenate all strings
1e4H$Y$     % compute 1/123456789101112... as a string with 1e4 significant digits
t32>)       % remove unwanted spaces and newlines in the output string
I0h)        % remove the first two characters ('8.')
j           % input string
Xf          % find indices of occurrences of input string within computed string
1)          % take first index
q           % subtract 1
\$\endgroup\$
2
\$\begingroup\$

Ruby, 69 67 bytes

->x{"#{1/BigDecimal.new(?.+[*1..9999]*'')}".index(x)-3}

Score: 55 source code bytes + 12 for the -rbigdecimal flag.

Pretty straightforward. Only weird part is that the to_s implicitly called by the #{} construction on BigDecimal returns

0.81000000670 [...] 3036E1

so it's necessary to subtract 3 to get the correct index.

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

Python 2, 69 bytes

lambda s:str(10**30000/int("".join(map(str,range(3000))))).index(s)-1
\$\endgroup\$
  • \$\begingroup\$ This won't work for an input of "8". \$\endgroup\$ – negative seven Jun 9 at 16:29

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.