The continued fraction factorization method (**CFRAC**) is a general-purpose factorization algorithm valid for integers. It calculates factors of a given integer number without considering its unique properties. It has a sub-exponential running time. It was first described in 1931 by D. H. Lehmer and R. E. Powers and later in 1975 were developed into a computer algorithm by Michael A. Morrison and John Brillhart.

**Continued Fraction:**

An expression that can be expressed in the form:

(1)

is called a Continued Fraction, where a_{i} and b_{i }are either real or complex values for all *i > = 0*. When all the values of b_{i}‘s_{ }are 1, then it is called a simple continued fraction.

A Simple Continued Fraction can be denoted as:

(2)

where C_{k}= [a_{0; }a_{1}, a_{2}, …, a_{n}] for k<=n is the k-th convergent of the Simple Continued Fraction.

An Infinite Continued Fraction [a_{0}; a_{1}, a_{2}, …, a_{k, }…] is defined as a limit of the convergents C_{k}=[a0; a1, a2, …, an]

**Algorithm:**

This algorithm uses residues produced in the Continued Fraction of (mn)^{1/2} for some m to produce a square number.

This algorithm solves the mathematical equation:

(3)

this equation is solved by calculating the value of m such that m

^{2 }(mod(n)) has the minimum upperbound.

- CFRAC algorithm has a time complexity of:

(4)

**Example 1:**

Input: continued_fraction((10/7)) Output: [1, 2, 3]Explanation:

(5)

**Example 2:**

Input: list(continued_fraction_convergents([0, 2, 1, 2])) Output: [0, 1/2, 1/3, 3/8]Explanation:

(6)

**Example 3:**

Input: continued_fraction_reduce([1, 2, 3, 4, 5]) Output: 225/157Explanation:

(7)

**Implementation:**

**Code: To convert a fraction into Continued Fraction representation**

`#using sympy module ` `from` `sympy.ntheory.continued_fraction ` `import` `continued_fraction ` `from` `sympy ` `import` `sqrt ` `#calling continued_fraction method ` `continued_fraction(` `10` `/` `7` `) ` |

*chevron_right*

*filter_none*

**Output:**

[1, 2, 3]

**Code 2: To convert a Continued Fraction into fraction.**

`#using sympy module ` `from` `sympy.ntheory.continued_fraction ` `import` `continued_fraction_reduce ` ` ` `#calling continued_fraction_reduce method ` `continued_fraction_reduce([` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]) ` |

*chevron_right*

*filter_none*

**Output:**

225/157

**Code 3: To get a list of convergents from a Continued fraction.**

`# using sympy module ` `from` `sympy.core ` `import` `Rational, pi ` `from` `sympy ` `import` `S ` `from` `sympy.ntheory.continued_fraction ` `import` `continued_fraction_convergents, continued_fraction_iterator ` `# calling continued_fraction_convergents method and ` `# passing it as a parameter to a list ` `list` `(continued_fraction_convergents([` `0` `, ` `2` `, ` `1` `, ` `2` `])) ` |

*chevron_right*

*filter_none*

**Output:**

[0, 1/2, 1/3, 3/8]

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Get the QR factorization of a given NumPy array
- Mahotas - Fraction of zeros in image
- as_integer_ratio() in Python for reduced fraction of a given rational
- Fraction module in Python
- Bisect Algorithm Functions in Python
- Page Rank Algorithm and Implementation
- Simplex Algorithm - Tabular Method
- Asynchronous Advantage Actor Critic (A3C) algorithm
- Cristian's Algorithm
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- k-nearest neighbor algorithm in Python
- ML | T-distributed Stochastic Neighbor Embedding (t-SNE) Algorithm
- ML | Mini Batch K-means clustering algorithm
- ML | Reinforcement Learning Algorithm : Python Implementation using Q-learning
- Silhouette Algorithm to determine the optimal value of k
- Implementing DBSCAN algorithm using Sklearn
- ML | ECLAT Algorithm
- Implementing Apriori algorithm in Python
- Explanation of Fundamental Functions involved in A3C algorithm
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.