Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
m (→{{header|Racket}}: Some improvements) |
|||
Line 479: | Line 479: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<lang racket> |
<lang racket> |
||
#lang racket |
|||
(define (r2cf n d) |
(define ((r2cf n d)) |
||
( |
(or (zero? d) |
||
(let-values ([(q r) (quotient/remainder n d)]) |
|||
(if (zero? d) |
|||
(set! n d) |
|||
( |
(set! d r) |
||
q))) |
|||
⚫ | |||
⚫ | |||
(define (r->cf n d) |
(define (r->cf n d) |
||
(for/list ([i (in-producer (r2cf n d) |
(for/list ([i (in-producer (r2cf n d) #t)]) i)) |
||
i)) |
|||
(define (real->cf x places) |
(define (real->cf x places) |
||
( |
(define d (expt 10 places)) |
||
(define n (exact-floor (* x d))) |
|||
(r->cf n d)) |
|||
⚫ | |||
(list 1 3 23 13 22 -151) |
|||
(list 2 1 8 11 7 77)) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
(real->cf (sqrt 2) 10) |
(real->cf (sqrt 2) 10) |
||
(real->cf pi 10) |
(real->cf pi 10) |
||
</lang> |
|||
{{out}} |
{{out}} |