# Find Almost Integers with High-Precision Arithmetic

Algorithms for finding integer relations [1], such as the PSLQ algorithm, require high-precision arithmetic to produce accurate results. The higher the precision of the inputs to the algorithm, the greater the level of confidence that the algorithm can find an integer relation that is not just a numerical artifact. The PSLQ algorithm can encounter false positives, such as those caused by almost integers.

This example shows how to find almost integers, or numbers that are very close to integers, using variable-precision arithmetic in Symbolic Math Toolbox™. This example searches for almost integers (or near-integers) that have the form $\mathrm{exp}\left(\pi \sqrt{\mathit{n}}\right)$ or $\mathrm{exp}\left(\pi \text{\hspace{0.17em}}\mathit{n}\right)$ for the integers $\mathit{n}=1,\text{\hspace{0.17em}}...,200$.

First, consider a well-known example of an almost integer [2] that is the real number $\mathrm{exp}\left(\pi \sqrt{163}\right)$. Create this real number as an exact symbolic number.

r = exp(pi*sqrt(sym(163)))

`r = $${\mathrm{e}}^{\pi \hspace{0.17em}\sqrt{163}}$$`

Evaluate this number with variable-precision arithmetic using `vpa`

. By default, `vpa`

calculates values to 32 significant digits.

vpa(r)

`ans = $$262537412640768743.99999999999925$$`

You can change the number of significant digits to a higher precision by using `digits`

. Evaluate the same number to 40 significant digits.

digits(40) vpa(r)

`ans = $$262537412640768743.9999999999992500725972$$`

This number is very close to an integer. Find the difference between this real number and its nearest integer. Use `vpa`

to evaluate this result to 40 significant digits.

dr = vpa(round(r)-r)

`dr = $$0.0000000000007499274028018143151532171817442410122968$$`

Next, search for almost integers that have the form $\mathrm{exp}\left(\pi \sqrt{\mathit{n}}\right)$ for the integers $\mathit{n}=1,\text{\hspace{0.17em}}...,200$. Create these numbers as exact symbolic numbers.

A = exp(pi*sqrt(sym(1:200)));

Set the number of significant digits to the number of digits in the integer part of $\mathrm{exp}\left(\pi \sqrt{200}\right)$ plus 20 decimal points.

d = log10(A(end)); digits(ceil(d) + 20)

Evaluate the differences between this series of numbers and their nearest integers. Find the almost integers with rounding errors that are less than 0.0001. Show the results in exact symbolic form.

B = vpa(round(A)-A); A_nearint = A(abs(B)<0.0001)'

A_nearint =$$\left(\begin{array}{c}{\mathrm{e}}^{\pi \hspace{0.17em}\sqrt{37}}\\ {\mathrm{e}}^{\pi \hspace{0.17em}\sqrt{58}}\\ {\mathrm{e}}^{\pi \hspace{0.17em}\sqrt{67}}\\ {\mathrm{e}}^{\pi \hspace{0.17em}\sqrt{163}}\end{array}\right)$$

Evaluate the almost integers with a precision of at least 20 decimal points.

A_nearint = vpa(A_nearint)

A_nearint =$$\left(\begin{array}{c}199148647.9999780465518567665009238753359\\ 24591257751.99999982221324146957619235527\\ 147197952743.9999986624542245068292613126\\ 262537412640768743.9999999999992500725972\end{array}\right)$$

Plot the histogram of the differences to show their distribution. The distribution has many occurrences of differences that are close to zero, where the form $\mathrm{exp}\left(\pi \sqrt{\mathit{n}}\right)$ is an almost integer.

histogram(double(B),40)

Next, search for almost integers that have the form $\mathrm{exp}\left(\pi \text{\hspace{0.17em}}\mathit{n}\right)$ for the integers $\mathit{n}=1,\text{\hspace{0.17em}}...,200$. Create these numbers as exact symbolic numbers.

A = exp(sym(pi)*1:200);

Set the number of significant digits to the number of digits in the integer part of $\mathrm{exp}\left(\pi \cdot 200\right)$ plus 20 decimal points.

d = log10(A(end)); digits(ceil(d) + 20)

Evaluate the differences between this series of numbers and their nearest integers. Find the almost integers with rounding errors that are less than 0.0001. The result is an empty `sym`

array, which means there is no almost integer that satisfies this condition.

B = vpa(round(A)-A); A_nearint = A(abs(B)<0.0001)

A_nearint = Empty sym: 1-by-0

Plot the histogram of the differences. The histogram is relatively evenly distributed and shows that the form $\mathrm{exp}\left(\pi \text{\hspace{0.17em}}\mathit{n}\right)$ does not have many occurrences of almost integers. For this specific example, there is no almost integer with a rounding error that is less than 0.0001.

histogram(double(B),40)

Finally, restore the default precision of 32 significant digits for further calculations.

digits(32)

### References

[1] “Integer Relation Algorithm.” In Wikipedia, April 9, 2022. https://en.wikipedia.org/w/index.php?title=Integer_relation_algorithm&oldid=1081697113.

[2] “Almost Integer.” In Wikipedia, December 4, 2021. https://en.wikipedia.org/w/index.php?title=Almost_integer&oldid=1058543590.