On Thu, Jan 16, 2014 at 07:34:41AM -0500, Marc Feeley wrote:
On Jan 16, 2014, at 4:59 AM, Estevo euccastro@gmail.com wrote:
The algorithm I gave produces the decimal representation such that the repeating part if there is one is identified. A simple change to the algorithm will return a boolean indicating if there is a repeating part or not (i.e. if the digit generator encounters a cycle).
Marc
(define (has-finite-decimal-representation? n) ;; n is a positive real
(define (generator frac states) (if (= frac 0) #t (let ((seen (member frac states))) (if seen #f (let* ((frac*10 (* 10 frac)) (digit (truncate frac*10))) (generator (- frac*10 digit) (cons frac states)))))))
(let* ((exact (inexact->exact n)) (int (truncate exact))) (generator (- exact int) '())))
(pp (has-finite-decimal-representation? 1/1024)) (pp (has-finite-decimal-representation? 1/999)) (pp (has-finite-decimal-representation? 5/700)) (pp (has-finite-decimal-representation? 0.1)) (pp (has-finite-decimal-representation? 0.2)) (pp (has-finite-decimal-representation? 0.3)) (pp (has-finite-decimal-representation? (* 4 (atan 1)))) (pp (has-finite-decimal-representation? (expt 1/3 7)))
;; output: ;; ;; #t ;; #f ;; #f ;; #t ;; #t ;; #t ;; #t ;; #f
If all you want is a boolean, maybe it would be simpler to reduce the fraction to lowest terms (might this already have been done?) and then checking if the denominator is divisible by 2 or 5.
-- hendrik