converging average of loop?

43 Views Asked by At

I ran the following script, It starts with a random number, then takes it's reciprocal and gets it's fractional part, Then it repeats it again, and takes average.

var x = Math.random();
var i = 0;
var k = 0;
var n; //number of iterations

while (i<n) {
x = (1/x)%1;
k+=x;
i++;
}

console.log(k/n);

The outputs where as follows

when n=100, output=0.45473379363722793
when n=1000, output=0.4517287948011644
when n=10000, output=0.4411420299062774
when n=100000, output=0.4423720018398649
when n=1000000, output=0.4423947214693864
when n=10000000, output=0.442541609651593

Thus the output seems to converge around something like 0.442..

Is there a method to find the average for infinite iterations (theoretically)?

Is there any significance of that number?

1

There are 1 best solutions below

1
On BEST ANSWER

The limit is the mean of the invariant probability measure on $[0,1]$ for the Gauss map $x \mapsto 1/x - \lfloor 1/x \rfloor$. That invariant measure has density $\dfrac{1}{\ln(2)(1+x)}$, and mean $\dfrac{1}{\ln(2)}-1 \approx 0.4426950409$.