# Khinchin's constant bad estimate [closed]

Inspired by How to write down numbers having an infinity of decimals? Link ðŸ‡«ðŸ‡·

## Background

From Wikipedia: for almost all real numbers $$\x\$$, coefficients $$\a_i\$$ of the continued fraction expansion of $$\x\$$ have a finite geometric mean that is independent of the value of $$\x\$$ and is known as Khinchin's Constant.

Khinchin's Constant can be calculated using the following method:

1. Using the $$\n\$$ first terms of the simple continued fraction of a real number (For example Pi).
2. Compute their product
3. Apply the $$\n^{\text{th}}\$$ root on the absolute value of product calculated above

## What is a simple continued fraction

It can be expressed as: $$x=[a_0; a_1, a_2, \dots, a_n]$$ Or $$x=a_0+ \cfrac{1}{a_1+ \cfrac{1}{a_2+ \cfrac{1}{\ddots{+ \cfrac{1}{a_n}}}}}$$

And can be calculated using the following:

1. Separate $$\x\$$ into its integer part $$\a_n\$$ and decimal part $$\d\$$
• $$\a_n = \lfloor x \rfloor\$$
• $$\d = x - \lfloor x \rfloor\$$
2. Repeat using the inverse of $$\d\$$ in place of $$\x\$$ while $$\d\$$ is not 0

It is a simple continued fraction because the numerator is always 1.

For negative numbers, the same rule applies, the first term of the sequence will be negative.

For example for $$\Ï€\$$: $$Ï€ = [a_0; a_1, a_2, a_3, \dots] = [3; 7, 15, 1, 292, \dots]$$

$$Ï€=3+\cfrac{1}{7+ \cfrac{1}{15+ \cfrac{1}{1+ \cfrac{1}{292+ \cdots}}}}$$

And $$\-Ï€ = [-4; 1, 6, 15, 1, 292, \dots]\$$

## Challenge

Using the method used to approximate Khinchin's Constant described above:
Given a number of terms $$\n\$$ and a real number $$\x\$$, compute the geometric mean of the coefficients of the continued fraction expansion of $$\x\$$.

Standard rules apply.

## Test cases:

Terms to use Real number Expected Result Note
1 Ï€ 3
2 Ï€ 4.5825...
3 Ï€ 6.8040...
7 Ï€ 5.1179...
7 -Ï€ 5.2165...
15 â„¯ 1.8156...
15 -â„¯ 1.9164...
20 -Ï† 1.0717...
20 Ï† 1
100 Ï† 1 Optional, might not work because of consecutive floating point errors

# Scoring

This is , so the answer with the least amount of bytes wins.

• Can we work with floating-point numbers and accept some small inaccuracy derived from that? Commented Sep 29, 2021 at 22:05
• Which invalidates my answer. \o/ Commented Sep 29, 2021 at 22:08
• I think the problem with phi using floating-point numbers may be in defining the input, rather than in the computations. phi cannot be defined exactly as a floating-point value Commented Sep 29, 2021 at 22:39
• Being pedantic, the challenge isnâ€™t to calculate Khinchinâ€™s constant but rather the (related) geometric mean of the coefficients of the continued fraction expansion of $x$. According to Wikipedia, $\phi$ and $e$ are among the exceptions to the â€˜almost allâ€™ clause: the geometric means for these numbers do not converge to Khinchinâ€™s constant. None of this changes the task as specified, but perhaps the wording could be clarified. Commented Sep 29, 2021 at 23:39
• To add to Luis Mendo's comment, I believe that -pi has a very different continued fraction expansion from pi if we adhere to wikipedia's definition, so (7,-pi) should give a very different answer from (7,pi). Commented Sep 30, 2021 at 12:50

# Husk, 14 bytes

^\Â¹Î â†‘Â¡Â§,o\%1âŒŠÂ²


Try it online! for the last test case (or try it here for n=7, number=pi).

     Â¡          # Apply function repeatedly to first results,
# collecting second results into infinite list:
Â§,     Â²  #  combine pair of results of functions applied to arg 2:
âŒŠ   #   floor
o\%1    #   reciprocal of fractional part
â†‘           # Now take arg1 elements from list,
Î             # calculate the product,
^               # and raise to the power of
\Â¹             # reciprocal of arg1


# Factor + math.continued-fractions math.unicode, 66 bytes

[ 1vector over [ dup next-approx ] times 1 head* Î  abs nth-root ]


Try it online!

## Explanation

It's a quotation (anonymous function) that takes an integer signifying the number of terms to use and a real number and returns a real number. Assuming 3 3.141592653589793 is on the data stack when this quotation is called...

Snippet Comment Data stack (top on right)
1vector Make a vector out of the object on top of the data stack. This is how next-approx expects to take its input. 3 V{ 3.141592653589793 }
over Put a copy of the object second from the top on top of the data stack. 3 V{ 3.141592653589793 } 3
[ dup next-approx ] Push a quotation to the data stack for times to use later. 3 V{ 3.141592653589793 } 3 [ dup next-approx ]
times Take an integer and a quotation and call the quotation that many times. In this case, equivalent to dup next-approx dup next-approx dup next-approx
Inside the quotation now... 3 V{ 3.141592653589793 }
dup Copy the top data stack object 3 V{ 3.141592653589793 } V{ 3.141592653589793 }
next-approx Add the next term in the continued fraction to our vector. next-approx has stack effect ( seq -- ) so we made a copy so we don't lose it 3 V{ 3 7.062513305931052 }
dup next-approx Iteration 2 3 V{ 3 7 15.9965944066841 }
dup next-approx Iteration 3 3 V{ 3 7 15 1.003417231015 }
1 head* Remove last element 3 V{ 3 7 15 }
Î  Take the product 3 315
abs Take the absolute value 3 315
nth-root Take the nth root of a number. In this case, take the cube root of 315 6.804092115953367

# Mathematica, 42 38 bytes

N@1##&@@ContinuedFraction[#2,#]^(1/#)&


Corrected formatting and size reduced by @theorist.

Inputting n=100 and variable as Pi we get the output as

2.69405


You can save 2 bytes by removing N@ but this will give an exact expression and not numeric.

The code also passes all the tests giving the exact value for each number except for -Pi for which it returns a complex number. However the magnitude of this complex number is exactly the same as that given in the expected value.

• For this site, you'll want to format your code as a program that can take the arguments as input, e.g.: N@1##&@@ContinuedFraction[#2,#]^(1/#)& (38 bytes). This would be implemented as: N@1##&@@ContinuedFraction[#2,#]^(1/#)&@@{n,x}, e.g., N@1##&@@ContinuedFraction[#2,#]^(1/#)&@@{100,Pi} Commented Sep 30, 2021 at 6:55
• You can remove the whitespace Commented Sep 30, 2021 at 6:57
• Thanks. I'll fix the formatting. Commented Sep 30, 2021 at 6:58

# JavaScript (ES7), 47 bytes

Expects (n)(real).

n=>v=>(g=n=>n?~~v*g(n-1,v=1/(v%1)):1)(n)**(1/n)


Try it online!

Note: With 40+ terms, the last test case will diverge from 1 because of cumulated floating point errors.

# Ruby, 57 bytes

->n,l,r=1{n=n.abs;l.times{r*=n.to_i**(1.0/l);n=1/n%=1};r}


Try it online!