racket/collects/math/private/number-theory/farey.rkt
Neil Toronto 96d1400654 Renamed functions
* bernoulli -> bernoulli-number
 * farey -> farey-sequence
 * fibonacci/mod -> modular-fibonacci
 * order -> unit-group-order
 * orders -> unit-group-orders

Documented `make-fibonacci' and `make-modular-fibonacci'

Reworked text about loading external libraries in docs for `math/bigfloat'

Removed type aliases like Z, Q, Prime (I like them, but TR was printing them
in unexpected places like array return types)
2012-11-27 22:23:42 -07:00

23 lines
764 B
Racket

#lang typed/racket/base
(require "types.rkt")
(provide farey-sequence mediant)
(: mediant : Exact-Rational Exact-Rational -> Exact-Rational)
(define (mediant x y)
(/ (+ (numerator x) (numerator y))
(+ (denominator x) (denominator y))))
(: farey-sequence : Integer -> (Listof Exact-Rational))
(define (farey-sequence n)
(cond [(n . <= . 0) (raise-argument-error 'farey-sequence "Positive-Integer" n)]
[else
(let loop ([a 1] [b 1] [c (sub1 n)] [d n] [#{fs : (Listof Exact-Rational)} '()])
(let ([fs (cons (/ a b) fs)])
(cond [(positive? a)
(define k (quotient (+ n b) d))
(loop c d (- (* k c) a) (- (* k d) b) fs)]
[else
fs])))]))