This repository contains a Java program that explores a rational sequence designed to approximate square roots of positive integers.
The idea originated from a topology of metric space course, where I was challenged to find a rational sequence that converges to the square root of two. Later, inspired by Euler’s method for approximating square roots, I derived a general expression for computing square roots using a sequence.
To verify its accuracy and convergence properties, I implemented this Java program to generate and analyze the sequence for different values. Since manual calculations quickly became impractical (e.g., for n=5, the sequence produces extremely large values), this program automates the process and provides insights into its behavior.
A more detailed theoretical discussion, including mathematical proofs, is available in a separate LaTeX-written article in another repository which is for now still private for some reasons.
This project is a research-based implementation of a rational sequence that
approximates the square root of any positive number.
It combines mathematical rigor and high-performance Java code to
demonstrate how a dense subset of ℚ (rational numbers) can be used to approach
any real number √n with extreme precision.
In this repository, I'm just going to cover the algorithm part of the main idea.
This program is written in Java 22 and uses Maven as dependency manager. Make sure you have JDK 22 installed before running it.
In addition to being written in Java 22 and using Maven as a dependency manager,
this program also relies on GMP (GNU Multiple Precision Arithmetic Library) and
JNA (Java Native Access). Instead of using BigInteger.gcd
, the program
leverages GMP for high-performance arithmetic operations, particularly for
computing greatest common divisors (GCD) efficiently. This significantly
improves performance when dealing with very large numbers in fraction
operations.
To properly run this program, make sure the following dependencies are installed on your system:
-
Windows: Install GMP using MSYS2 or manually compile it.
-
Linux:
-
Debian/Ubuntu:
sudo apt install libgmp-dev
-
Arch Linux:
sudo pacman -S gmp
-
Fedora:
sudo dnf install gmp-devel
-
-
MacOS: Install GMP via Homebrew:
brew install gmp
Additionally, JNA is managed via Maven, so no manual installation is required. However, ensure that your Java environment is correctly set up to load native libraries.
Indeed, as we are using Java while dealing with rational numbers, I preferred to implement my owned version of what we define by rational number in programming : fractions.
The program provides an object called Fraction which represents fraction in mathematical world.
So by doing :
Fraction oneHalf = new Fraction(1,2);
It will be similar on doing :
The program provides some default fraction such as Fraction.ZERO
and
Fraction.ONE
which are the zero fraction and the one fraction.
In mathematical world, the Fraction.ZERO
is just :
And the Fraction.ONE
is just :
The fraction Fraction
acts exactly as a mathematical fraction does. So, all
operation provided by the filed :
is available.
To verify the accuracy of the sequence, we compare our results with high-precision values from reliable external sources.
All test data used in this repository come from trusted mathematical sources:
- NASA's Astronomy Picture of the Day (APOD) numerical data repository
Here are the data files and their sources:
File Name | Source |
---|---|
sqrt_2.txt |
NASA - sqrt2.10mil |
sqrt_3.txt |
NASA - sqrt3.1mil |
sqrt_5.txt |
NASA - sqrt5.1mil |
sqrt_6.txt |
NASA - sqrt6.1mil |
sqrt_7.txt |
NASA - sqrt7.1mil |
These sources provide extremely precise approximations of well-known mathematical constants. Since NASA rely on precise calculations for research and space exploration, their datasets are considered highly accurate.
By using these sources, we ensure that the program's results are tested against verified, high-precision values, reinforcing the reliability of the algorithm.
4/5 of the tests found in the
test package have exactly
ONE MILLION
(1,000,000
) decimal places. The test for square root of 2 is
about SIX MILLION
(6,000,000
) decimal places.
Mathematical remarks about the tests:
As we can notice in tests done in the test package, we can see that :
About the SquareRoots
SquareRoot
and SquareRootSub
implement an abstract class called Sequence
,
which is the super class of all sequences that are going to be used in this
program. That SquareRoot
class is where This repository focuses in.
According to its implementation, we can find the expression of the sequence in
the SquareRoots
kThValue
method which gives us the k-th value of the square root sequence. The
natural mathematical notation will be
The mathematical expression of this sequence is the following :
Then the final sequence that gives us the square root of m is the following :
Where the SquareRootSub
class represents the sequence
and the SquareRoot
class represents the sequence
Author: Abegà Razafindratelo