# Computing only the N-th Digit of Pi

Status
Not open for further replies.

#### Juggler_IN

##### Active Member
I need help in fixing a C to VBA conversion code.

The code outputs the specified n-th digit of pi in base-10 (Decimal System) on the lines of the Bailey–Borwein–Plouffe formula which outputs the specified n-th digit of pi in base-16 (Hexadecimal System).

The converted, but not functional, VBA code and the reference C code are enclosed.

A fully-functional execution of the C code can be viewed at tio.run which is an online C compiler.

VBA Code:
``````Sub Main0()
' Expected output for i = 1 to 50 is 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 4 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5 1 0 5.

For i = 0 To 50
Debug.Print NthPi(i) & " ";
Next i

End Sub
Function NthPi(ByVal d As Integer) As Long

Dim ll, j, i

ll = Int((d + 2) * 10 / 3 + 2)
j = 0
i = 0

Dim x, r

ReDim x(0 To ll)
ReDim r(0 To ll)

Do
x(j) = 20
j = j + 1
Loop While j < ll

Dim c, n, e, p

p = 0

Do While i < d

j = 0
c = 0

Do
n = ll - j - 1
e = n * 2 + 1
r(j) = (x(j) + c) Mod e
c = (x(j) / e) * n
j = j + 1
Loop While j < ll

ll = ll - 1
p = x(ll) / 10
r(ll) = x(ll + 1) Mod 10

j = 0

Do
x(j) = r(j) * 10
j = j + 1
Loop While j < ll

i = i + 1

Loop

NthPi = p Mod 10

End Function``````

Code:
``````namespace System.Linq
{
class P
{
static void Main()
{
for (int i = 0; i <= 50; ++i)
{
Console.Write(NthPi(i));
}

Console.WriteLine();
}

static long NthPi(int d)
{
int l = (d+=2) * 10 / 3 + 2, j = 0, i = 0;

long[] x = new long[l], r = new long[l];

for (; j < l;)
x[j++] = 20;

long c, n, e, p = 0;

for (; i < d; ++i)
{
for (j = 0, c = 0; j < l; c = (x[j++] / e) * n)
{
n = l - j - 1;
e = n * 2 + 1;

r[j] = (x[j] += c) % e;
}

p = x[--l] / 10;

r[l] = x[l++] % 10;

for (j = 0; j < l;)
x[j] = r[j++] * 10;
}

return p % 10;
}
}
}``````

Cross-posted at: ExcelForum.com
An alternate code (which I don't want to pursue) cross-posted at: MrExcel.com

### Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},\$Z\$1:\$Z\$99,\$Y\$1:\$Y\$99),2,False) to lookup Y values to left of Z values.

#### Fluff

##### MrExcel MVP, Moderator
Duplicate to: Nth digit of Pi

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

Status
Not open for further replies.

Replies
1
Views
359
Replies
3
Views
84
Replies
1
Views
171
Replies
11
Views
240
Replies
0
Views
89

1,191,535
Messages
5,987,157
Members
440,082
Latest member
belodelokelo

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back