[ create a new paste ] login | about

Link: http://codepad.org/eopIofz7    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jun 3:
1
2
3
4
5
6
7
8
9
10
11
12
; egyptian fractions

(define (egypt n d)
  (let loop ((n n) (d d) (xs (list)))
    (if (= n 1) (reverse (cons d xs))
      (let* ((x (ceiling (/ d n))) (y (- (/ n d) (/ x)))
             (n (numerator y)) (d (denominator y)))
        (loop n d (cons x xs))))))

(display (egypt 5 6)) (newline)
(display (egypt 7 15)) (newline)
(display (egypt 5 121)) (newline)


Output:
1
2
3
(2 3)
(3 8 120)
(25 757 763309 873960180913 1527612795642093418846225)


Create a new paste based on this one


Comments: