Also, I must correct this statement :

Originally Posted by
Juggler_IN
@6StringJazzer; The code calculates the n-th digit of pi. So, instead of computing the whole value to the n-th place, it just computes the nth place digit.
The C code is compute not just the n-th place, but all from 0 to n-th place.
For example, if you call NthPi(10), actually NthPi(0), NthPi(1), NthPi(2), NthPi(3), ... until NthPi(10) is calculated, so you are not just compute at exact n-th place.
This means, if you run the loop like this :
For i = 0 To 5
Debug.Print NthPi(i)
Next i
when i = 0, nth(0) is calculated
when i = 1, nth(0) is recalculated, nth(1) is calculated
when i = 2, nth(0) is recalculated, nth(1) is recalculated, nth(2) is calculated
when i = 3, nth(0) is recalculated, nth(1) is recalculated, nth(2) is recalculated, nth(3) is calculated
when i = 4, nth(0) is recalculated, nth(1) is recalculated, nth(2) is recalculated, nth(3) is recalculated, nth(4) is calculated
etc
To show using the code :
Here we "only want the 10th" place (notice no loop, just single item 10) :
Sub Main()
Dim i As Long
i = NthPi(10)
End Sub
We insert the Debug line to show what is going on behind the scene :
Function NthPi(ByVal d As Long) As Long
'int l = (d+=2) * 10 / 3 + 2, j = 0, i = 0;
Dim L As Long, j As Long, i As Long
d = d + 2
L = d * 10 \ 3 + 2
'long[] x = new long[l], r = new long[l];
Dim x() As Long, r() As Long
ReDim x(L), r(L)
'for (; j < l;)
' x[j++] = 20;
For j = 0 To L - 1
x(j) = 20
Next j
'long c, n, e, p = 0;
Dim c As Long, n As Long, e As Long, p As Long
'for (; i < d; ++i)
'{
For i = 0 To d - 1
'for (j = 0, c = 0; j < l; c = (x[j++] / e) * n)
'{
c = 0
For j = 0 To L - 1
'n = l - j - 1;
n = L - j - 1
'e = n * 2 + 1;
e = n * 2 + 1
'r[j] = (x[j] += c) % e;
x(j) = x(j) + c
r(j) = x(j) Mod e
'}
c = (x(j) \ e) * n
Next j
'p = x[--l] / 10;
L = L - 1
p = x(L) \ 10
Debug.Print "The " & i; " digit = " & p
'r[l] = x[l++] % 10;
r(L) = x(L) Mod 10
L = L + 1
'for (j = 0; j < l;)
For j = 0 To L - 1
'x[j] = r[j++] * 10;
x(j) = r(j) * 10
'}
Next j
'}
Next i
'return p % 10;
NthPi = p Mod 10
End Function
The result of macro is :
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
The 8 digit = 5
The 9 digit = 3
The 10 digit = 5
The 11 digit = 8
Which means, even though we input the 10 as n-th place, but actually all the digit between 0 to 10 is calculated, it's only not shown up to user (hidden).
Thus the statement "it just computes the nth place digit" is wrong.
Now, if put it back again into loop, the result will be very clear:
Sub Main()
Dim i As Long, j As Long
For i = 0 To 10
j = NthPi(i)
Debug.Print "================="
Next i
End Sub
The 0 digit = 3
The 1 digit = 1
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
The 8 digit = 5
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
The 8 digit = 5
The 9 digit = 3
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
The 8 digit = 5
The 9 digit = 3
The 10 digit = 5
=================
The 0 digit = 3
The 1 digit = 1
The 2 digit = 4
The 3 digit = 1
The 4 digit = 5
The 5 digit = 9
The 6 digit = 2
The 7 digit = 6
The 8 digit = 5
The 9 digit = 3
The 10 digit = 5
The 11 digit = 8
=================
Bookmarks