# Continued Fraction calculator

1. Alpertron
2. Programs
3. Continued Fraction calculator

Any real number x can be represented uniquely by a continued fraction:

x is equal to a sub 0 plus 1 over a sub 1 plus 1 over a sub 2 plus 1 over a sub 3 plus etcetera

where a1, a2, a3, ... are integer numbers greater than zero. A more compact representation is:

x is equal to a sub 0 plus double slash a sub 1, a sub 2, a sub 3, etcetera double slash

If the number to be represented is rational, there is a finite number of terms in the continued fraction. If the number is a quadratic irrationality of the form fraction whether the numerator is a plus the square root of b and the denominator is c , then the continued fraction is periodic. This calculator can find the continued fraction expansions of rational numbers and quadratic irrationalities. Apart from the coefficients an, the program allows to find the convergent An/Bn. This quotient is the best rational approximation to the argument x with denominator less or equal to Bn and matches the value obtained by developing only the first n coefficients of the continued fraction.

You can type numbers or numerical expressions on the input boxes.

The calculator accepts numbers of up to 10000 digits.

If you need the square root to subtract the number at the left, just negate both a and c.

If b is negative, the result is not a real number, so it cannot be represented as a continued fraction.

For rational numbers the calculator finds the coefficients and the numerator and denominator of all convergents, but for quadratic irrationalities the calculator stops after the 100000th convergent if the period is larger. When the program displays convergents, it stops before if it runs out of memory.

## Expressions

You can use the following operators and parentheses for the expressions:

• - for subtraction
• * for multiplication
• / for integer division
• % for modulus (remainder of the integer division)
• ^ or ** for exponentiation (the exponent must be greater than or equal to zero).
• <, ==, >; <=, >=, != for comparisons. The operators return zero for false and -1 for true.
• AND, OR, XOR, NOT for binary logic. The operations are done in binary (base 2). Positive (negative) numbers are prepended with an infinite number of bits set to zero (one).
• SHL: When b ≥ 0, a SHL b shifts a left the number of bits specified by b. This is equivalent to a × 2b. Otherwise, a SHL b shifts a right the number of bits specified by −b. This is equivalent to floor(a / 2b). Example: 5 SHL 3 = 40.
• SHR: When b ≥ 0, a SHR b shifts a right the number of bits specified by b. This is equivalent to floor(a / 2b). Otherwise, a SHR b shifts a left the number of bits specified by −b. This is equivalent to a × 2b. Example: -19 SHR 2 = -5.
• n!: factorial (n must be greater than or equal to zero). Example: 6! = 6 × 5 × 4 × 3 × 2 = 720.
• p#: primorial (product of all primes less or equal than p). Example: 12# = 11 × 7 × 5 × 3 × 2 = 2310.
• B(n): Previous probable prime before n. Example: B(24) = 23.
• F(n): Fibonacci number Fn from the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. where each element equals the sum of the previous two members of the sequence. Example: F(7) = 13.
• L(n): Lucas number Ln = Fn-1 + Fn+1
• N(n): Next probable prime after n. Example: N(24) = 29.
• P(n): Unrestricted Partition Number (number of decompositions of n into sums of integers without regard to order). Example: P(4) = 5 because the number 4 can be partitioned in 5 different ways: 4 = 3+1 = 2+2 = 2+1+1 = 1+1+1+1.
• Gcd(m,n): Greatest common divisor of these two integers. Example: GCD(12, 16) = 4.
• Modinv(m,n): inverse of m modulo n, only valid when m and n are coprime, menaning that they do not have common factors. Example: Modinv(3,7) = 5 because 3 × 5 ≡ 1 (mod 7)
• Modpow(m,n,r): finds mn modulo r. Example: Modpow(3, 4, 7) = 4, because 34 ≡ 4 (mod 7).
• IsPrime(n): returns zero if n is not probable prime, -1 if it is. Example: IsPrime(5) = -1.
• NumDigits(n,r): Number of digits of n in base r. Example: NumDigits(13, 2) = 4 because 13 in binary (base 2) is expressed as 1101.
• SumDigits(n,r): Sum of digits of n in base r. Example: SumDigits(213, 10) = 6 because the sum of the digits expressed in decimal is 2+1+3 = 6.
• RevDigits(n,r): finds the value obtained by writing backwards the digits of n in base r. Example: RevDigits(213, 10) = 312.

You can use the prefix 0x for hexadecimal numbers, for example 0x38 is equal to 56.

## Algorithms used

The calculation of the coefficients of the continued fraction of a rational number is done as follows:

1. Obtain the first coefficient as the integer part of the quotient between the numerator and the denominator rounded down.
2. Subtract the numerator from the product of the denominator and the newly found coefficient.
3. While the numerator is not zero:
1. Exchange the numerator and the denominator.
2. Obtain the following coefficient as the integer part of the quotient between the numerator and the denominator rounded down.
3. Subtract the numerator from the product of the denominator and the newly found coefficient.

The PQa algorithm is used to calculate the coefficients in the case of quadratic irrationalities, which are quotients between numerator + delta and denominator.

1. Repeat indefinitely:
1. Get the coefficient as the integer part of the quotient between numerator + delta and the denominator rounded down.
2. Subtract the numerator from the product of the newly found coefficient and the denominator.
3. Change sign to the numerator.
4. Calculate an auxiliary result as the difference between delta and the square of the numerator.
5. Replace the denominator by the quotient between the previous auxiliary result and the denominator.

Based on the coefficients, the convergent numerators Ai and denominators Bi can be calculated using the following operations:

1. Initialize the current and previous convergent numerator with the values ​​1 and 0 respectively.
2. Initialize the current and previous convergent denominator with the values ​​0 and 1 respectively.
3. For each coefficient obtained:
1. Calculate an auxiliary result as the product of the coefficient by the current convergent numerator.
2. Obtain the next convergent numerator as the sum of the auxiliary result and the previous convergent numerator.
3. Show the newly found value.
4. Replace the previous convergent numerator with the current one.
5. Replace the current convergent numerator with the next one.
6. Perform the previous five steps replacing numerator by denominator.

## Source code

You can download the source of the current program and the old continued fraction applet from GitHub. Notice that the source code is in C language and you need the Emscripten environment in order to generate Javascript.

Written by Dario Alpern. Last updated 28 September 2020.