NTLib - Number Theory Library 0.9
Loading...
Searching...
No Matches
continued_fraction.cpp File Reference

Primary module interface unit for module continued_fraction. More...

#include <vector>
import rational;
import base;
Include dependency graph for continued_fraction.cpp:

Functions

template<Integer T>
std::vector< T > ntlib::quadratic_irrational_cf (const T &n)
 Continued fraction representation for a quadratic irrational.
template<Integer T>
rational< T > ntlib::nth_convergent_quadratic_irrational_cf (std::size_t n, const std::vector< T > &cf)

Detailed Description

Primary module interface unit for module continued_fraction.

Function Documentation

◆ nth_convergent_quadratic_irrational_cf()

template<Integer T>
rational< T > ntlib::nth_convergent_quadratic_irrational_cf ( std::size_t n,
const std::vector< T > & cf )
export

Computes the n-th convergent of the regular, periodic continued fraction for a quadratic irrational.

Algorithm from: http://mathworld.wolfram.com/PellEquation.html

Parameters
nThe n-th convergent is calculated.
cfThe continued fraction representation of the quadratic irrational.
Returns
The numerator and denominator of the n-th convergent.

◆ quadratic_irrational_cf()

template<Integer T>
std::vector< T > ntlib::quadratic_irrational_cf ( const T & n)
export

Continued fraction representation for a quadratic irrational.

Let \(n\) be a non-square natural number. This function template computes the continued fraction representation of \(\sqrt{n}\).

For non-squares (and only these) the continued fraction is regular and periodic.

Template Parameters
TAn integer-like type.
Parameters
nThe number n. Must not be a perfect square.
Returns
The regular, periodic continued fraction representing sqrt(n). The first position gives the whole number part, the second until the last one form the period.