Skip to content

Farey Sequence next term algorithm #1552

@jxu

Description

@jxu

Article: The Stern-Brocot Tree and Farey Sequences

Problem:

Surprisingly the page is missing a nice way of computing the next term in F_n without any recursion.

https://en.wikipedia.org/wiki/Farey_sequence#Next_term

Given two consecutive entries a/b and c/d, the next term p/q is given by

p = ((n + b) // d) * c - a
q = ((n + b) // d) * d - b

Start with a/b = 0/1 and c/d = 1/n.

I don't have a good source of this info other than wikipedia (who doesn't cite anything) but it's also here as Properties of Farey Sequences 5. https://dummit.cos.northeastern.edu/teaching_sp21_4527/4527_lecture_03_farey_sequences_v2.pdf

Also I should mention the number of terms at the end is 1 + Phi(n), where Phi is the totient summatory function, Phi(x) ~ (3 / pi^2) x^2 + O(x ln x).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions