# Find Almost Integers Using High-Precision Arithmetic

This example shows how to find *almost integers*, or numbers that are very close to integers, using variable-precision arithmetic in Symbolic Math Toolbox™. In this example, you search for almost integers that have the form `exp(pi*sqrt(n))`

or `exp(pi*n)`

for the integers `n`

= 1, ..., 200.

### Almost Integers

By default, MATLAB® uses 16 digits of precision. For higher precision, use the `vpa`

function in Symbolic Math Toolbox. `vpa`

provides variable precision, which can be increased when evaluating numbers.

First, consider a well-known example of an almost integer [2] that is the real number `exp(pi*sqrt(163))`

. Create this real number as an exact symbolic number.

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

r = exp(pi*163^(1/2))

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

. By default, `vpa`

evaluates values to 32 significant digits.

f = vpa(r)

f = 262537412640768743.99999999999925

You can change the number of significant digits by using the `digits`

function. Evaluate the same number to 25 significant digits.

digits(25) f = vpa(r)

f = 262537412640768744.0

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

to evaluate the difference to 25 significant digits.

dr = vpa(round(r)-r)

dr = 0.0000000000007425171588693046942353249

### Almost Integers of Form `exp(pi*sqrt(n)`

Search for almost integers that have the form `exp(pi*sqrt(n))`

for the integers `n`

= 1, ..., 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 `exp(pi*sqrt(200))`

plus 20 more digits.

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

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

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

A_nearint = exp(pi*37^(1/2)) exp(pi*58^(1/2)) exp(pi*67^(1/2)) exp(pi*163^(1/2))

Plot a histogram of the differences. Their distribution shows many occurrences of differences that are close to zero, where the form `exp(pi*sqrt(n))`

is an almost integer.

histogram(double(B),100)

### Almost Integers of Form `exp(pi*n`

Search for almost integers that have the form `exp(pi*n)`

for the integers `n`

= 1, ..., 200. Create these numbers as exact symbolic numbers.

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

Set the number of significant digits to the number of digits in the integer part of `exp(pi*200)`

plus 20 more digits.

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

Evaluate the differences between these 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 no number in this series satisfies this condition.

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

A_nearint = Empty sym: 1-by-0

Plot a histogram of the differences. The histogram, which is relatively evenly distributed, shows that the form `exp(pi*n)`

does not produce many occurrences of almost integers. For this specific example, no almost integer has a rounding error less than 0.0001.

histogram(double(B),100)

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.