Skip to main content

farey

Tests PyPI version Python License: MIT

Generate Farey sequences and Farey ranges.

Overview

The Farey sequence F_n is the sequence of all completely reduced fractions between 0 and 1, with denominators ≤ n, in ascending order.

A Farey range farey_range(x, y, n) gives every rational number with denominator ≤ n inside the interval [x, y]. Equivalently, it is the (sorted) union of all the rational ranges whose reciprocal steps are the elements of the Farey sequence F_n.

The result is exact: a list of Fraction objects computed with the Farey next-term recurrence (no floating-point interval scaling). This is a faithful port of the improved Wolfram Language FareyRange used internally by the GeneralizedRange paclet, extended to negative and unit-fraction steps.

farey_sequence

Usage

from farey import farey_sequence

farey_sequence(1)
# [Fraction(0, 1), Fraction(1, 1)]

farey_sequence(5)
# [Fraction(0, 1), Fraction(1, 5), Fraction(1, 4), Fraction(1, 3),
#  Fraction(2, 5), Fraction(1, 2), Fraction(3, 5), Fraction(2, 3),
#  Fraction(3, 4), Fraction(4, 5), Fraction(1, 1)]

farey_range

Usage

from farey import farey_range
from fractions import Fraction

farey_range(0, 1, 3)
# [Fraction(0, 1), Fraction(1, 3), Fraction(1, 2), Fraction(2, 3), Fraction(1, 1)]

farey_range(2, 4, 3)   # bounds greater than 1
# [Fraction(2, 1), Fraction(7, 3), Fraction(5, 2), Fraction(8, 3), Fraction(3, 1),
#  Fraction(10, 3), Fraction(7, 2), Fraction(11, 3), Fraction(4, 1)]

farey_range(0, Fraction(3, 2), 2)   # exact, non-integer bound
# [Fraction(0, 1), Fraction(1, 2), Fraction(1, 1), Fraction(3, 2)]

# Descending: first bound larger, negative step
farey_range(1, 0, -3)
# [Fraction(1, 1), Fraction(2, 3), Fraction(1, 2), Fraction(1, 3), Fraction(0, 1)]

Parameters

Parameter Default Description
start (required) First bound — the starting point of the range
end (required) Second bound
step 1 Controls the Farey order and direction (see below)

Step conventions

The magnitude of step selects the Farey order; the sign selects the direction. n and 1/n are equivalent (both mean order n):

Step value Order Direction
n (positive int) n ascending
1/n (float or Fraction(1, n)) n ascending
-n (negative int) n descending
-1/n (float or Fraction(-1, n)) n descending

A step that is zero, or a non-unit fraction such as 2/3, raises FareyError.

Performance

farey_range produces the same output as the Wolfram Language FareyRange resource function (verified case-for-case). Because the work is pure exact-rational arithmetic — with no parsing hot-path to exploit — the compiled Wolfram kernel is about 3× faster on large ranges, while the Python port wins on tiny ones; the trade-off is dependency-free portability. See benchmark/BENCHMARK.md for the full table and methodology.

See also

This Python package has been inspired by the following Wolfram functions:

License

MIT

Metadata

Release files for farey 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for farey 0.7.0
File Size Uploaded
farey-0.7.0.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for farey 0.7.0
File Interpreter ABI Platform
farey-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.2 kB

Release files / farey-0.7.0.tar.gz

Download URL farey-0.7.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bc731d1500d71a6df11824852c93b0e623c3b549b200d7a1b5667ad7adf8a935
BLAKE2b-256 checksum
How to use checksums
cb7661f9279af1e6db948a022fd21b56bbd97f131317dc91a9ddeafa35bcc98b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / farey-0.7.0-py3-none-any.whl

Download URL farey-0.7.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3dfea7fe75e995e35dc6128ad0e261ef3df51bb051732bce8e3ad1cd7daf346
BLAKE2b-256 checksum
How to use checksums
a32a2d47bac08fbc9fe76ee01611e7d34a5db5e2e5439fca9ac948dd0cccac67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page