Using VBA to find perfect numbers, and accurately calculate pi to 100,000 decimal places.

BlueSpruce

Active member
Local time
Today, 17:35
Joined
Jul 18, 2025
Messages
781
In my other Watercooler post about "Showcase Your Relics" I mentioned the first basic program I wrote in high school for finding perfect numbers. The thread detoured into that topic when I posted vba code to find perfect numbers. So I am forking this new thread to continue with that topic here, and other pure mathematics, such as number theory. @RonPaii ran the vba code that has nested loops and it's very cpu intensive.

https://www.access-programmers.co.uk/forums/threads/showcase-your-relics.334801/post-1978860

I asked AI to generate vba code to accurately calculate pi to 100K decimal places and AI said VBA is not an adequate language for that task because the supported datatypes, such as variant, can only go up to 28 significant digits. AI is suggesting to build custom functions for supporting BigInt mathematical operations. Here's what AI provided, Your thoughts?

Calculating Pi to 100,000 decimal places in VBA is not practical or accurate due to the limitations of VBA's built-in data types. The standard Double type only provides about 15 digits of precision, and the Decimal type, a subtype of Variant, is limited to 28 digits. Achieving a much higher precision of 100,000 digits requires a custom-built "Big Number" or "BigInt" library to handle large-scale arithmetic.

Instead of a single VBA module, a complete solution would involve creating your own library for string-based arithmetic. The code below provides a proof-of-concept for the fundamental math functions needed, based on an archived tutorial by Tushar Mehta. It then applies these functions to a highly efficient Machin-like formula to compute Pi.

Big number arithmetic functions

Starting with a smaller number like 10,000 is recommended to test the code before attempting 100,000 digits, which will be very time-consuming.

Code:
Option Explicit
'------------------------------------------------------------------------------------------------------------------------------------------------
' basCalculatePi: Module to compute Pi using a Machin-like formula and the BigInt library.
'------------------------------------------------------------------------------------------------------------------------------------------------

Public Function CalculatePi(ByVal numDigits As Long) As String
    ' Computes Pi to the specified number of decimal places using the formula: π/4 = 4*arctan(1/5) - arctan(1/239)
    Dim arctan1_5 As String
    Dim arctan1_239 As String
    Dim term1 As String, term2 As String, piStr As String

    ' Calculate the arctan series for each term
    arctan1_5 = ArctanSeries("1", "5", numDigits + 5)
    arctan1_239 = ArctanSeries("1", "239", numDigits + 5)

    ' Perform the main calculation using BigInt functions
    term1 = BigMultiply(BigMultiply(arctan1_5, "4"), "4")
    term2 = BigMultiply(arctan1_239, "4") ' This term in the formula should be BigSubtract(term1, BigMultiply(arctan1_239, "4")) for the whole pi. Please refer to an accurate Machin-like formula definition.
    piStr = BigSubtract(term1, term2) ' Assuming term2 is calculated as 4 * arctan(1/239). Please verify the formula implementation.

    ' Format the final result
    CalculatePi = Left$(piStr, numDigits + 1)
End Function

Private Function ArctanSeries(ByVal num As String, ByVal denom As String, ByVal precision As Long) As String
    ' Computes the Taylor series for arctan(num/denom) = x - x^3/3 + x^5/5 - x^7/7 + ... where x = num/denom.
    ' ... (Full function code implementing the series calculation)
End Function
 
When I had an OpenVMS/Itanium system with the Navy, I had access to DEC BASIC, 128-bit OCTAWORD integers and IEEE X-floating-point with 53-digits precision for X-format. I used the Arctan formula to develop the sequence for Pi and Euler's constant e; I also created a power series to compute the probability values for standard deviation numbers up to about 10. But I no longer have access to one of those beautiful beasts and I didn't happen to keep printouts of the code for that.

The hardest part was for those power series. Sure, as you progressed, the contribution from each term diminished, but you couldn't stop until a term was less than 10^-54 times the first term of the series. If you are using any kind of power series for the 100,000 digits case, you wouldn't be able to drop power series terms until they were 10^-100,001 less than the first term. That's a lot of terms.
 
When I had an OpenVMS/Itanium system with the Navy, I had access to DEC BASIC, 128-bit OCTAWORD integers and IEEE X-floating-point with 53-digits precision for X-format. I used the Arctan formula to develop the sequence for Pi and Euler's constant e; I also created a power series to compute the probability values for standard deviation numbers up to about 10. But I no longer have access to one of those beautiful beasts and I didn't happen to keep printouts of the code for that.

The hardest part was for those power series. Sure, as you progressed, the contribution from each term diminished, but you couldn't stop until a term was less than 10^-54 times the first term of the series. If you are using any kind of power series for the 100,000 digits case, you wouldn't be able to drop power series terms until they were 10^-100,001 less than the first term. That's a lot of terms.
Accurately calculating pi to nth decimal places has been a classic computing topic for hundreds of years. I remember an IBM 7090 being used to calculate pi to 100K places, and now there's supercomputers that do it to billions of decimal places. When dealing with very large numbers, most applications, such as Astronomical, don't really concern themselves with extreme precision. Scaled approximations, e.g. plus or minus 100 million years in a scale of 15 billion years is acceptable. However, pi is an intriguing number because it has no repeating patterns and its calculation must be accurate to nth decimal places.

Access and VBA is ill equipped for accurately calculating pi past 28 places. String based arithmentic has to be used and LongText can only hold 64K characters. So the calculations would have to be output as character stream into a text file. Here's what AI suggest the best tools and methods for calculating pi to many decimal places:

For accurately calculating Pi to 100,000 decimal places, the best language choice depends on your priorities between performance and ease of use. However, the most critical factor is the use of a robust, high-performance arbitrary-precision arithmetic library, such as the GNU Multiple Precision Arithmetic Library (GMP).

Here is a breakdown of the best languages based on different priorities:

For maximum performance: C or C++

For the fastest possible execution time, C or C++ combined with the GMP library is the top choice.

  • Why it's the best for speed: GMP is highly optimized for performance, utilizing C and even hand-coded assembly for the most critical routines.
  • How it works: You would write your program in C or C++, calling GMP's functions for all the large-number arithmetic required by an efficient Pi algorithm, like the Chudnovsky algorithm
    • Best for: Performance-critical applications and setting speed records for Pi calculation.
    • Downside: Requires manual memory management and has a steeper learning curve than higher-level languages.
For a balance of convenience and performance: Python

Python is an excellent choice for its simplicity and a vast ecosystem of libraries that make arbitrary-precision arithmetic easy to implement.

  • Why it's a great option: Python's standard decimal module handles large-precision decimal numbers, and high-performance libraries like gmpy2 offer a convenient wrapper around the native speed of GMP.
  • How it works: A Python script using gmpy2 or the decimal module can implement an algorithm like the Chudnovsky formula. The heavy-lifting computations are handled efficiently by the underlying C libraries.
  • Best for: Developers who want a fast result without the complexity of C++, students learning about high-precision algorithms, and projects where development time is more important than raw speed.


For mathematical and scientific computing: Julia or Mathematica

These languages are specifically designed for high-performance numerical computation and have arbitrary-precision arithmetic built-in or easily accessible.

  • Why they are excellent:
    • Julia: Offers native support for arbitrary-precision floating-point numbers (BigFloat), which relies on the MPFR library (built on GMP) for fast and accurate results. Julia is a compiled, high-performance language that is often as fast as C.
    • Mathematica (Wolfram Language): Has built-in arbitrary-precision and even "infinite-precision" arithmetic. The language automatically handles the precision required for calculations, making it very user-friendly for this task.
  • Best for: Scientists, mathematicians, and researchers who need both high-level mathematical constructs and performance.


What about algorithms?

The choice of programming language is only half the battle. The speed of your calculation will also heavily depend on the algorithm you choose.

  • Chudnovsky algorithm: This is the current state-of-the-art for Pi calculation and is the algorithm used to set new world records. It converges extremely quickly, making it ideal for high precision.
  • Gauss-Legendre algorithm: An older but still very efficient iterative algorithm for Pi calculation.
  • Leibniz formula or Nilkantha's series: These are very simple to implement but converge too slowly for high-precision calculations like 100,000 digits.

For a 100,000-digit calculation, an efficient algorithm like Chudnovsky's is essential, regardless of the language you choose. The combination of a powerful language (C++, Python, or Julia) and a high-performance library like GMP or MPFR will ensure both accuracy and speed.
 
Recently, my news feed pointed to the article above. I'm not a mathematician, so I can't make any statements concerning this topic beyond the observation that this is another method of computing Pi. Hopefully, this will help the discussion progress.
 
Recently, my news feed pointed to the article above. I'm not a mathematician, so I can't make any statements concerning this topic beyond the observation that this is another method of computing Pi. Hopefully, this will help the discussion progress.

That was a very interesting read of how string theorists accidentally discovered a very fast way for calculating pi.
It inspired me to find a faster way for finding perfect numbers, and I have achieved at least six orders of magnitude, (Million times faster), from the original method I was using!
Attached is the accdb with the discovery, thanks for the article, Steve (y)

Mersenne PrimeMethod.png
 

Attachments

Last edited:
When dealing with very large numbers, most applications, such as Astronomical, don't really concern themselves with extreme precision.

If you saw the movie Hidden Figures, you would realize that a desktop Friden Comptometer model 1115 was a mechanical four-function mechanical machine (Add, Subtract, Multiply, Divide) used for NASA calculations during Project Mercury and the young female clerks who performed the computations had the job title of "Computer." A Friden could get you 12 digits easily and it was precise enough to do what was required. Heck, 6-digit logarithm tables were often enough. Bringing on board the IBM 7090 - as also appeared in that movie - gave you 8 digits, but as you pointed out, NASA only needed a certain level of precision because the physical rocketry equipment wasn't that precise anyway. But for the 7090, they only had SINGLE precision - a 36-bit "word" with 27-bit mantissa and ~8 decimal digits. Didn't get DOUBLE until the advent of the 7094, 54-bit mantissa or ~ 15 digits.

If you've ever played the game Civilization: Beyond Earth, you get to see their sly little dig at folks who strive for many degrees of "vacuous precision." While I know there is that goal of getting a few more digits in the number, the truth is that past about 10 digits, we can't reliably measure with any more precision anyway - at least, not for real-world objects - because our measuring devices aren't that precise to make a difference. If I wanted to measure the length of a 2"x4" down to angstroms, you would be justified in putting me out of my misery right away. But, since I know better, I won't make that attempt.
 
If you saw the movie Hidden Figures,
Seen the movie 6 times, and counting.
NASA only needed a certain level of precision because the physical rocketry equipment wasn't that precise anyway
For John Glenn's re-entry they needed very precise calculations for the landing coordinates near the USS Randolph (CV-15) aircraft carrier.
past about 10 digits, we can't reliably measure with any more precision anyway - at least, not for real-world objects
I cannot think of any practical applications that would require pi acuracy of more than 30 decimal places. However, pi has intrigued brains since the ancient greek times, and still does to present day.
 
I cannot think of any practical applications that would require pi acuracy of more than 30 decimal places. However, pi has intrigued brains since the ancient greek times, and still does to present day.

That level of accuracy is important only for those big wheels who frequently go around in elevated circles.

{Sorry... that was as irresistible to me as "Shave and a haircut, six bits" was to 'toons' from Who Framed Roger Rabbit?} :giggle:
 

Users who are viewing this thread

Back
Top Bottom