Farey Sequence
Last Updated :
12 Jul, 2022
Farey sequence is a sequence which is generated for order n. The sequence has all rational numbers in range [0/0 to 1/1] sorted in increasing order such that the denominators are less than or equal to n and all numbers are in reduced forms i.e., 4/4 cannot be there as it can be reduced to 1/1.
Examples:
F1 = 0/1, 1/1
F2 = 0/1, 1/2, 1/1
F3 = 0/1, 1/3, 1/2, 2/3, 1/1
.
.
F7 = 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5,
3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5,
5/6, 6/7, 1/1
Farey sequence is used in rational approximations of irrational numbers, ford circles and
in Riemann hypothesis (See this for more details)
How to generate Farey Sequence of given order?
The idea is simple, we consider every possible rational number from 1/1 to n/n. And for every generated rational number, we check if it is in reduced form. If yes, then we add it to Farey Sequence. A rational number is in reduced form if GCD of numerator and denominator is 1.
Below is the implementation based on above idea.
C++
#include <bits/stdc++.h>
using namespace std;
class Term {
public :
int x, y;
Term( int x, int y)
: x(x), y(y)
{
}
};
bool cmp(Term a, Term b)
{
return a.x * b.y < b.x * a.y;
}
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void farey( int n)
{
vector<Term> v;
for ( int i = 1; i <= n; ++i) {
for ( int j = i + 1; j <= n; ++j)
if (gcd(i, j) == 1)
v.push_back(Term(i, j));
}
sort(v.begin(), v.end(), cmp);
cout << "0/1 " ;
for ( int i = 0; i < v.size(); ++i)
cout << v[i].x << "/" << v[i].y << " " ;
cout << "1/1" ;
}
int main()
{
int n = 7;
cout << "Farey Sequence of order " << n << " is\n" ;
farey(n);
return 0;
}
|
Java
import java.util.*;
class Term {
public int x, y;
public Term( int x1, int y1)
{
this .x = x1;
this .y = y1;
}
};
public class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void farey( int n)
{
ArrayList<Term> v = new ArrayList<Term>();
for ( int i = 1 ; i <= n; ++i) {
for ( int j = i + 1 ; j <= n; ++j)
if (gcd(i, j) == 1 )
v.add( new Term(i, j));
}
Collections.sort(v, new Comparator<Term>() {
public int compare(Term a, Term b)
{
return a.x * b.y - b.x * a.y;
}
});
System.out.print( "0/1 " );
for ( int i = 0 ; i < v.size(); ++i)
System.out.print(v.get(i).x + "/" + v.get(i).y
+ " " );
System.out.print( "1/1" );
}
public static void main(String[] args)
{
int n = 7 ;
System.out.print( "Farey Sequence of order " + n
+ " is\n" );
farey(n);
}
}
|
Python3
class Term:
def __init__( self , x, y):
self .x = x
self .y = y
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def farey(n):
v = []
for i in range ( 1 , n + 1 ):
for j in range (i + 1 , n + 1 ):
if gcd(i, j) = = 1 :
v.append(Term(i, j))
for i in range ( len (v)):
for j in range (i + 1 , len (v)):
if (v[i].x * v[j].y > v[j].x * v[i].y):
v[i], v[j] = v[j], v[i]
print ( "0/1" , end = " " )
for i in range ( len (v)):
print ( "%d/%d" % (v[i].x,
v[i].y), end = " " )
print ( "1/1" )
if __name__ = = "__main__" :
n = 7
print ( "Farey sequence of order %d is" % n)
farey(n)
|
C#
using System;
using System.Collections.Generic;
class TermComparer : Comparer<Term>
{
public override int Compare(Term a, Term b)
{
return a.x * b.y - b.x * a.y;
}
}
class Term {
public int x, y;
public Term( int x1, int y1)
{
this .x = x1;
this .y = y1;
}
};
public class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void farey( int n)
{
List<Term> v = new List<Term>();
for ( int i = 1; i <= n; ++i) {
for ( int j = i + 1; j <= n; ++j)
if (gcd(i, j) == 1)
v.Add( new Term(i, j));
}
v.Sort( new TermComparer());
Console.Write( "0/1 " );
for ( int i = 0; i < v.Count; ++i)
Console.Write(v[i].x + "/" + v[i].y + " " );
Console.Write( "1/1" );
}
public static void Main( string [] args)
{
int n = 7;
Console.Write( "Farey Sequence of order " + n
+ " is\n" );
farey(n);
}
}
|
Javascript
<script>
class Term
{
constructor(x, y)
{
this .x = x;
this .y = y;
}
}
function gcd(a, b)
{
if (b == 0)
return a
return gcd(b, a % b)
}
function farey(n)
{
let v = []
for (let i=1;i<n + 1;i++)
{
for (j=i+1;j<n + 1;j++)
{
if (gcd(i, j) == 1)
v.push( new Term(i, j))
}
}
for (let i=0;i<(v).length;i++)
{
for (let j=i + 1; j<(v).length; j++)
{
if (v[i].x * v[j].y > v[j].x * v[i].y)
{
let temp = v[j];
v[j] = v[i];
v[i] = temp;
}
}
}
document.write( "0/1 " )
for (let i=0;i<(v).length;i++)
document.write(v[i].x+ "/" +v[i].y+ " " )
document.write( "1/1" )
}
let n = 7;
document.write( "Farey sequence of order " +n+ " is<br>" );
farey(n)
</script>
|
Output:
Farey Sequence of order 7 is
0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7
3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1
Time complexity of above approach is O(n2 Log n) where O(log n) is an upper bound on time taken by Euclid’s algorithm for GCD.
Farey Sequence has below properties [See wiki for details]
A term x/y can be recursively evaluated using previous two terms. Below is the formula to compute xn+2/yn+2 from xn+1/yn+1 and xn/yn.
x[n+2] = floor((y[n]+n) / y[n+1])x[n+1]– x[n]
y[n+2] = floor((y[n]+n) / y[n+1])y[n+1]– y[n]
We can use above properties to optimize.
C++
#include <bits/stdc++.h>
using namespace std;
void farey( int n)
{
double x1 = 0, y1 = 1, x2 = 1, y2 = n;
printf ( "%.0f/%.0f %.0f/%.0f" , x1, y1, x2, y2);
double x, y = 0;
while (y != 1.0) {
x = floor ((y1 + n) / y2) * x2 - x1;
y = floor ((y1 + n) / y2) * y2 - y1;
printf ( " %.0f/%.0f" , x, y);
x1 = x2, x2 = x, y1 = y2, y2 = y;
}
}
int main()
{
int n = 7;
cout << "Farey Sequence of order " << n << " is\n" ;
farey(n);
return 0;
}
|
Java
class GFG
{
static void farey( int n)
{
double x1 = 0 , y1 = 1 , x2 = 1 , y2 = n;
System.out.printf( "%.0f/%.0f %.0f/%.0f" , x1, y1, x2, y2);
double x, y = 0 ;
while (y != 1.0 )
{
x = Math.floor((y1 + n) / y2) * x2 - x1;
y = Math.floor((y1 + n) / y2) * y2 - y1;
System.out.printf( " %.0f/%.0f" , x, y);
x1 = x2;
x2 = x;
y1 = y2;
y2 = y;
}
}
public static void main(String[] args)
{
int n = 7 ;
System.out.print( "Farey Sequence of order " + n + " is\n" );
farey(n);
}
}
|
Python3
import math
def farey(n):
x1 = 0 ;
y1 = 1 ;
x2 = 1 ;
y2 = n;
print (x1, end = "")
print ( "/" , end = "")
print (y1, x2, end = "")
print ( "/" , end = "")
print (y2, end = " " );
x = 0 ;
y = 0 ;
while (y ! = 1.0 ):
x = math.floor((y1 + n) / y2) * x2 - x1;
y = math.floor((y1 + n) / y2) * y2 - y1;
print (x, end = "")
print ( "/" , end = "")
print (y, end = " " );
x1 = x2;
x2 = x;
y1 = y2;
y2 = y;
n = 7 ;
print ( "Farey Sequence of order" , n, "is" );
farey(n);
|
PHP
<?php
function farey( $n )
{
$x1 = 0;
$y1 = 1;
$x2 = 1;
$y2 = $n ;
echo $x1 , "/" , $y1 ,
" " , $x2 , "/" ,
$y2 , " " ;
$x ;
$y = 0;
while ( $y != 1.0)
{
$x = floor (( $y1 + $n ) / $y2 ) * $x2 - $x1 ;
$y = floor (( $y1 + $n ) / $y2 ) * $y2 - $y1 ;
echo $x , "/" , $y , " " ;
$x1 = $x2 ;
$x2 = $x ;
$y1 = $y2 ;
$y2 = $y ;
}
}
$n = 7;
echo "Farey Sequence of order " , $n , " is\n" ;
farey( $n );
?>
|
C#
using System;
public class GFG
{
static void farey( int n)
{
double x1 = 0, y1 = 1, x2 = 1, y2 = n;
Console.Write( "{0:F0}/{1:F0} {2:F0}/{3:F0}" , x1, y1, x2, y2);
double x, y = 0;
while (y != 1.0)
{
x = Math.Floor((y1 + n) / y2) * x2 - x1;
y = Math.Floor((y1 + n) / y2) * y2 - y1;
Console.Write( " {0:F0}/{1:F0}" , x, y);
x1 = x2;
x2 = x;
y1 = y2;
y2 = y;
}
}
public static void Main(String[] args)
{
int n = 7;
Console.Write( "Farey Sequence of order " + n + " is\n" );
farey(n);
}
}
|
Javascript
<script>
function farey(n)
{
var x1 = 0, y1 = 1, x2 = 1, y2 = n;
document.write(x1+ "/" +y1+ " " +x2+ "/" +y2+ " " );
var x, y = 0;
while (y != 1.0)
{
x = Math.floor((y1 + n) / y2) * x2 - x1;
y = Math.floor((y1 + n) / y2) * y2 - y1;
document.write(x+ "/" + y+ " " );
x1 = x2;
x2 = x;
y1 = y2;
y2 = y;
}
}
var n = 7;
document.write( "Farey Sequence of order " + n + " is<br/>" );
farey(n);
</script>
|
Output:
Farey Sequence of order 7 is
0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7
3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1
Time Complexity of this solution is O(n)
References:
https://en.wikipedia.org/wiki/Farey_sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...