racket/collects/math/private/number-theory/quadratic.rkt
Neil Toronto f2dc2027f6 Initial math library commit. The history for these changes is preserved
in the original GitHub fork:

  https://github.com/ntoronto/racket

Some things about this are known to be broken (most egregious is that the
array tests DO NOT RUN because of a problem in typed/rackunit), about half
has no coverage in the tests, and half has no documentation. Fixes and
docs are coming. This is committed now to allow others to find errors and
inconsistency in the things that appear to be working, and to give the
author a (rather incomplete) sense of closure.
2012-11-16 11:39:51 -07:00

31 lines
1.0 KiB
Racket

#lang typed/racket/base
(require racket/list
"types.rkt")
(provide quadratic-solutions
quadratic-integer-solutions
quadratic-natural-solutions)
(: quadratic-solutions : Real Real Real -> (Listof Real))
(define (quadratic-solutions a b c)
; return list of solutions to a a x^2 + b x + c = 0
(let ([d (- (* b b) (* 4 a c))])
(cond
[(< d 0) '()]
[(= d 0) (list (/ b (* -2 a)))]
[else
(let ([sqrt-d (sqrt d)])
(list (/ (- (- b) sqrt-d) (* 2 a))
(/ (+ (- b) sqrt-d) (* 2 a))))])))
(: quadratic-integer-solutions : Real Real Real -> (Listof Integer))
(define (quadratic-integer-solutions a b c)
; return list of integer solutions to a x^2 + b x + c = 0
(filter exact-integer? (quadratic-solutions a b c)))
(: quadratic-natural-solutions : Real Real Real -> (Listof Natural))
(define (quadratic-natural-solutions a b c)
; return list of nonnegative-integer solutions to a x^2 + b x + c = 0
(filter natural? (quadratic-solutions a b c)))