Abdulaziz Ghuloum wrote:
> Is it possible that you actually wanted exact-integer-sqrt?
In the case of the factorization function sqrt was used
used to break out of a loop early, so it doesn't
matter which one of sqrt and integer-sqrt is used here.
Grepping for sqrt reveals the following use, where
it is not possible to replace sqrt with integer-sqrt.
(define (second-degree-solutions a b c)
; return list of solutions to 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))))])))
It is used like this to examine whether a number is
triangular, pentagonal, etc.
(define (second-degree-natural-solutions a b c)
; return list of integer solutions to a x^2 + b x + c = 0
(filter natural? (second-degree-solutions a b c)))
Abdulaziz Ghuloum wrote:
> Is it possible that you actually wanted exact-integer-sqrt?
In the case of the factorization function sqrt was used
used to break out of a loop early, so it doesn't
matter which one of sqrt and integer-sqrt is used here.
Grepping for sqrt reveals the following use, where
it is not possible to replace sqrt with integer-sqrt.
(define (second- degree- solutions a b c)
(/ (+ (- b) sqrt-d) (* 2 a))))])))
; return list of solutions to 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))
It is used like this to examine whether a number is
triangular, pentagonal, etc.
(define (natural? n)
(and (integer? n) (positive? n)))
(define (second- degree- natural- solutions a b c) degree- solutions a b c)))
; return list of integer solutions to a x^2 + b x + c = 0
(filter natural? (second-
(define (triangle? n) degree- natural- solutions 1/2 1/2 (- n)))))
(not (null? (second-
(define (pentagonal? n) degree- natural- solutions 3/2 -1/2 (- n)))))
(not (null? (second-
Hmm. I suppose one could use the following definition of s instead
of sqrt.
(define (square n)
(* n n))
(define (s n)
(let ([sqrt-n (integer-sqrt n)])
(if (= (square sqrt-n) n)
sqrt-n
(sqrt (exact->inexact n)))))
/Jens Axel