Translation Issue with a Java to VBA Code - BBP-Bellard Formula

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
This Java (and the subsequent VBA) program at the link below extracts the nth digit of Pi without calculating the digits before it. It will print out the nth number followed by the next 9 numbers in base 10. (It is an implementation of BBP through Bellard's formula.)

Java Code: BBP Formula Code

The java code compiles successfully at the online compiler Code Compiler.

I have attempted an exact translation of the Java code but, it is not executing. Either I am missing a loop; which I am unable to figure out.



Nth Decimal.png


My Code
VBA Code:
Sub pi_Java()
' Note: I have modified capital N in java code to m lowercase n stays as is.

    Dim n&

    Dim av&, a&, vmax&, m&, num&, den&, k&, kq&, kq2&, t&, v&, s&, i&

    Dim sum#

    n = 1    ' Modify (At 1, decimal digits of pi at position 1: 141592653)

    m = Int(((n + 20) * Log(10) / Log(2)))
    ' Debug.Print "m=" & m

    sum = 0

    For a = 3 To (2 * m) - 1
        Debug.Print "a=" & a

        vmax = Int(Log(2 * m) / Log(a))
        av = 1

        For i = 0 To vmax - 1
            av = av * a
        Next

        s = 0
        num = 1
        den = 1
        v = 0
        kq = 1
        kq2 = 1

        For k = 1 To m

            t = k

            If (kq >= a) Then

                Do
                    t = t / a
                    v = v - 1
                Loop Until ((t Mod a) = 0)

                kq = 0

            End If

            kq = kq + 1
            num = mulMod(num, t, av)

            t = 2 * k - 1

            If (kq2 >= a) Then

                If (kq2 = a) Then

                    Do
                        t = t / a
                        v = v - 1
                    Loop Until ((t Mod a) = 0)

                End If

                kq2 = kq2 - a

            End If

            den = mulMod(den, t, av)
            kq2 = kq2 + 2

            If (v > 0) Then
                t = modInverse(den, av)
                t = mulMod(t, num, av)
                t = mulMod(t, k, av)
                For i = v To vmax - 1
                    t = mulMod(t, a, av)
                Next
                s = s + t
                If (s >= av) Then s = s - av
            End If

        Next k

        t = powMod(10, n - 1, av)
        s = mulMod(s, t, av)
        Debug.Print sum, s, av
        sum = (sum + s / av) Mod 1
        ' Debug.Print "Sum=" & sum

        a = nextPrime(a)

    Next a

    Debug.Print "SUM=" & sum
    Debug.Print Int(sum * 1000000000#)

End Sub
Function isPrime(n&) As Boolean
' /* This function returns true if n is prime. */

    Dim r&, i&

    If (n Mod 2&) = 0& Then
        isPrime = False
    Else
        r = Int(Sqr(n))
        For i = 3& To r Step 2&
            If (n Mod i) = 0& Then
                isPrime = False
            Else
                isPrime = True
            End If
        Next i
    End If

End Function
Function nextPrime&(n&)
' /* This function returns the prime number immediately after n. */

    Do
        n = n + 1&
    Loop Until Not isPrime(n)
    nextPrime = n

End Function
Function powMod&(a&, b&, m&)
' /* This function returns the (a^b) mod m. */

    Dim tempo&, temp&

    If (b = 0) Then
        tempo = 1
    ElseIf (b = 1) Then
        tempo = a
    Else
        temp = powMod(a, b / 2, m)
        If (b Mod 2 = 0) Then
            tempo = (temp * temp) Mod m
        Else
            tempo = ((temp * temp) Mod m) * a Mod m
        End If
    End If

    powMod = tempo

End Function
Function mulMod&(a&, b&, m&)
' /* This function returns (a*b) mod m */

    mulMod& = Int((a * b) Mod m)

End Function
Function modInverse&(a&, n&)
' /* This function returns the inverse of a mod n. */

Dim i&, v&, d&, t&, x&

        i = n
        v = 0
        d = 1
        
        Do While (a > 0)
            t = i / a
            x = a
            a = i Mod x
            i = x
            x = d
            d = v - t * x
            v = x
        Loop
        
        v = v Mod n
        
        If (v < 0) Then
            v = (v + n) Mod n
        Else
        End If
        
        modInverse = v

End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top