Updated.
This commit is contained in:
@@ -1,28 +1,31 @@
|
|||||||
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
||||||
;; about the language level of this file in a form that our tools can easily process.
|
;; about the language level of this file in a form that our tools can easily process.
|
||||||
#reader(lib "htdp-beginner-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
|
#reader(lib "htdp-beginner-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
|
||||||
; List of Number -> String
|
; Number a, Number b, Number c -> String
|
||||||
; Determines whether a list of 3 numbers is ascending or descending. Throws error if neither.
|
; Determines whether a list of 3 numbers is ascending or descending. Throws error if neither.
|
||||||
(define (scending-how l)
|
(define (scending-how a b c)
|
||||||
(cond [(< (first l) (second l) (third l)) "ascending"]
|
(cond [(< a b c) "ascending"]
|
||||||
[(> (first l) (second l) (third l)) "descending"]
|
[(> a b c) "descending"]
|
||||||
[else (error "too complex")]))
|
[else (error "too complex")]))
|
||||||
|
|
||||||
(check-expect (scending-how (list 1 2 3)) "ascending")
|
(check-expect (scending-how 1 2 3) "ascending")
|
||||||
(check-expect (scending-how (list 5 4 3)) "descending")
|
(check-expect (scending-how 5 4 3) "descending")
|
||||||
(check-error (scending-how (list 1 0 3)))
|
(check-error (scending-how 1 0 3))
|
||||||
|
|
||||||
|
|
||||||
; Number, Number -> Number
|
; Number a, Number b -> Number
|
||||||
; Finds two positive numbers' greatest common denominator.
|
; Finds two positive numbers' greatest common denominator.
|
||||||
(define (gcd2 a b)
|
(define (gcd2 a b)
|
||||||
(cond
|
(cond
|
||||||
[(= a 0) b]
|
[(= a 0) b]
|
||||||
[(= b 0) a]
|
[(= b 0) a]
|
||||||
[(= 0 (+ (modulo a 2) (modulo b 2))) (* 2 (gcd2 (/ a 2) (/ b 2)))]
|
[(and (even? a) (even? b))
|
||||||
[(= 0 (modulo b 2)) (gcd2 a (/ b 2))]
|
(* 2 (gcd2 (/ a 2) (/ b 2)))]
|
||||||
[(= 0 (modulo a 2)) (gcd2 (/ a 2) b)]
|
[(even? b) (gcd2 a (/ b 2))]
|
||||||
[else (if (<= a b) (gcd2 a (- b a)) (gcd2 b (- a b)))]))
|
[(even? a) (gcd2 (/ a 2) b)]
|
||||||
|
[else(if (<= a b)
|
||||||
|
(gcd2 a (- b a))
|
||||||
|
(gcd2 b (- a b)))]))
|
||||||
|
|
||||||
(check-expect (gcd2 0 0) 0)
|
(check-expect (gcd2 0 0) 0)
|
||||||
(check-expect (gcd2 100 0) 100)
|
(check-expect (gcd2 100 0) 100)
|
||||||
@@ -30,3 +33,13 @@
|
|||||||
(check-expect (gcd2 10 10) 10)
|
(check-expect (gcd2 10 10) 10)
|
||||||
(check-expect (gcd2 14 7) 7)
|
(check-expect (gcd2 14 7) 7)
|
||||||
(check-expect (gcd2 288 64) 32)
|
(check-expect (gcd2 288 64) 32)
|
||||||
|
|
||||||
|
; (gcd2 30 210)
|
||||||
|
; (* 2 (gcd2 15 105))
|
||||||
|
; (* 2 (gcd2 15 90))
|
||||||
|
; (* 2 (gcd2 15 45))
|
||||||
|
; (* 2 (gcd2 15 30))
|
||||||
|
; (* 2 (gcd2 15 15))
|
||||||
|
; (* 2 (gcd2 15 0))
|
||||||
|
; (* 2 15)
|
||||||
|
; 30
|
||||||
|
Reference in New Issue
Block a user