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:
- Using the \$n\$ first terms of the
simple continued fraction
of a real number (For example Pi). - Compute their product
- 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:
- Separate \$x\$ into its integer part \$a_n\$ and decimal part \$d\$
- \$a_n = \lfloor x \rfloor\$
- \$d = x - \lfloor x \rfloor\$
- 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 code-golf 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 code-golf, so the answer with the least amount of bytes wins.
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 \$\endgroup\$