Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

No edit summary
Line 477:
print(list(real2cf(Fraction(13, 11)))) # => [1, 5, 2]
print(list(islice(real2cf(2 ** 0.5), 20))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</lang>
 
=={{header|Racket}}==
<lang racket>#lang racket
 
(require racket/generator)
 
(define (r2cf n d)
(generator ()
(let loop ([n n] [d d])
(cond [(= d 0) null]
[else (let-values ([(q r) (quotient/remainder n d)])
(yield q)
(loop d r))]))))
 
(define (r->cf n d)
(for/list ([i (in-producer (r2cf n d) null)])
i))
 
(map r->cf
(list 1 3 23 13 22 -151)
(list 2 1 8 11 7 77))
 
(define (real->cf x places)
(let* ([d (expt 10 places)]
[n (exact-floor (* x d))])
(r->cf n d)))
 
(real->cf (sqrt 2) 10)
(real->cf pi 10)</lang>
 
=={{header|REXX}}==
Anonymous user