;Questions: ; 1) Does this code implement the greedy algorithm? ; 2) Does this greedy algorithm always terminate? ; 3) Does the greedy algorithm always give an ; optimal solution? ; 4) Is the greedy algorithm an efficient ; solution? (load "writeln.scm") (load "positiveInteger.scm") (define Egypt2 (lambda (n d testValue reciprocals) ;n: Numerator that remains after reciprocals are taken out. ;d: Denominator that remains after reciprocals are taken out. ;testValue: The current reciprocal to be tested. ;reciprocals: The list of reciprocals found thus far. (define d_new 0) (define n_new 0) (define n_remove 0) (writeln n d testValue reciprocals) (if (or (<= d n) (not (positiveInteger? n)) (not (positiveInteger? d)) ) (display 'error) (if (= n 1) (cons d reciprocals) (if (> (/ n d) (/ 1 testValue)) (begin (writeln (/ n d) (/ 1 testValue)) (writeln (exact->inexact (/ n d)) (exact->inexact (/ 1 testValue)) ) (set! d_new (lcm d testValue)) ;lcm: Least Common Multiple (writeln d_new) (set! n_remove (quotient d_new testValue)) (writeln n_remove) (set! n_new (- (* (quotient d_new d) n) n_remove)) (writeln n_new) (Egypt2 n_new d_new (+ testValue 1) (cons testValue reciprocals) ) ) (begin (writeln n d) (Egypt2 n d (+ testValue 1) reciprocals) ) ) ) ) )) (define Egypt (lambda (n d) ;n: numerator d: denominator (define reciprocals '()) (set! reciprocals (Egypt2 n d 2 ())) (display reciprocals) (newline) ) )