Generate a sequence of all positive rational numbers.
There are two main functions: nth-rational
and rational-seq
.
Returns nth rational (0-based) as ordered by a breadth-first navigation of the Calkin-Wilf tree: 1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...
Examples:
First
(nth-rational 0)
;;=> 1
Second
(nth-rational 1)
;;=> 1/2
Big Indices
(nth-rational 98254)
;;=> 179/40
(nth-rational 10000000000000000000000000000000000000000000000)
;;=> 100081852181957676623/97939718087223099718
Generates a lazy sequence of rational numbers following the same
order as nth-rational
with an optional starting index.
First 100 Rationals
Include them all:
(take 100 (rational-seq))
Skip the first 30:
(take 70 (rational-seq 30))
First Rational w/ Denominator Larger Than Max Integer Value
(->> (rational-seq 10000000000000000000)
(filter #(cond->> % (ratio? %) denominator :else (< Integer/MAX_VALUE)))
(take 1))
;;=> 1035723213/3048158966