Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Let's use the Algol 68 MOD operator after all) |
(Added a Scheme implementation.) |
||
Line 2,426: | Line 2,426: | ||
[3, 7, 476190, 3] |
[3, 7, 476190, 3] |
||
[3, 7, 7142857] |
[3, 7, 7142857] |
||
</pre> |
|||
=={{header|Scheme}}== |
|||
{{works with|Chez Scheme}} |
|||
'''The Implementation''' |
|||
<lang scheme>; Create a terminating Continued Fraction generator for the given rational number. |
|||
; Returns one term per call; returns #f when no more terms remaining. |
|||
(define make-continued-fraction-gen |
|||
(lambda (rat) |
|||
(let ((num (numerator rat)) (den (denominator rat))) |
|||
(lambda () |
|||
(if (= den 0) |
|||
#f |
|||
(let ((ret (quotient num den)) |
|||
(rem (modulo num den))) |
|||
(set! num den) |
|||
(set! den rem) |
|||
ret)))))) |
|||
; Return the continued fraction representation of a rational number as a string. |
|||
(define rat->cf-string |
|||
(lambda (rat) |
|||
(let* ((cf (make-continued-fraction-gen rat)) |
|||
(str (string-append "[" (format "~a" (cf)))) |
|||
(sep ";")) |
|||
(let loop ((term (cf))) |
|||
(when term |
|||
(set! str (string-append str (format "~a ~a" sep term))) |
|||
(set! sep ",") |
|||
(loop (cf)))) |
|||
(string-append str "]")))) |
|||
; Return the continued fraction representation of a rational number as a list of terms. |
|||
(define rat->cf-list |
|||
(lambda (rat) |
|||
(let ((cf (make-continued-fraction-gen rat)) |
|||
(lst '())) |
|||
(let loop ((term (cf))) |
|||
(when term |
|||
(set! lst (append lst (list term))) |
|||
(loop (cf)))) |
|||
lst)))</lang> |
|||
'''The Task''' |
|||
<br /> |
|||
Each continued fraction is displayed in both the conventional written form and as a list of terms. |
|||
<lang scheme>(printf "~%Basic examples:~%") |
|||
(for-each |
|||
(lambda (rat) |
|||
(printf "~a = ~a~%" rat (rat->cf-string rat)) |
|||
(printf "~a : ~a~%" rat (rat->cf-list rat))) |
|||
'(1/2 3 23/8 13/11 22/7 -151/77 0)) |
|||
(printf "~%Root2 approximations:~%") |
|||
(for-each |
|||
(lambda (rat) |
|||
(printf "~a = ~a~%" rat (rat->cf-string rat)) |
|||
(printf "~a : ~a~%" rat (rat->cf-list rat))) |
|||
'(14142/10000 141421/100000 1414214/1000000 14142136/10000000 141421356237/100000000000)) |
|||
(printf "~%Pi approximations:~%") |
|||
(for-each |
|||
(lambda (rat) |
|||
(printf "~a = ~a~%" rat (rat->cf-string rat)) |
|||
(printf "~a : ~a~%" rat (rat->cf-list rat))) |
|||
'(31/10 314/100 3142/1000 31428/10000 314285/100000 3142857/1000000 |
|||
31428571/10000000 314285714/100000000 31415926535898/10000000000000))</lang> |
|||
{{out}} |
|||
<pre> |
|||
Basic examples: |
|||
1/2 = [0; 2] |
|||
1/2 : (0 2) |
|||
3 = [3] |
|||
3 : (3) |
|||
23/8 = [2; 1, 7] |
|||
23/8 : (2 1 7) |
|||
13/11 = [1; 5, 2] |
|||
13/11 : (1 5 2) |
|||
22/7 = [3; 7] |
|||
22/7 : (3 7) |
|||
-151/77 = [-1; 25, 1, 2] |
|||
-151/77 : (-1 25 1 2) |
|||
0 = [0] |
|||
0 : (0) |
|||
Root2 approximations: |
|||
7071/5000 = [1; 2, 2, 2, 2, 2, 1, 1, 29] |
|||
7071/5000 : (1 2 2 2 2 2 1 1 29) |
|||
141421/100000 = [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2] |
|||
141421/100000 : (1 2 2 2 2 2 2 3 1 1 3 1 7 2) |
|||
707107/500000 = [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12] |
|||
707107/500000 : (1 2 2 2 2 2 2 2 3 6 1 2 1 12) |
|||
1767767/1250000 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2] |
|||
1767767/1250000 : (1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2) |
|||
141421356237/100000000000 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 4, 1, 2, 1, 63, 2, 1, 1, 1, 4, 2] |
|||
141421356237/100000000000 : (1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 1 4 1 2 1 63 2 1 1 1 4 2) |
|||
Pi approximations: |
|||
31/10 = [3; 10] |
|||
31/10 : (3 10) |
|||
157/50 = [3; 7, 7] |
|||
157/50 : (3 7 7) |
|||
1571/500 = [3; 7, 23, 1, 2] |
|||
1571/500 : (3 7 23 1 2) |
|||
7857/2500 = [3; 7, 357] |
|||
7857/2500 : (3 7 357) |
|||
62857/20000 = [3; 7, 2857] |
|||
62857/20000 : (3 7 2857) |
|||
3142857/1000000 = [3; 7, 142857] |
|||
3142857/1000000 : (3 7 142857) |
|||
31428571/10000000 = [3; 7, 476190, 3] |
|||
31428571/10000000 : (3 7 476190 3) |
|||
157142857/50000000 = [3; 7, 7142857] |
|||
157142857/50000000 : (3 7 7142857) |
|||
15707963267949/5000000000000 = [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 21, 17, 1, 1, 1, 1, 8, 1, 7, 2, 1, 2, 2] |
|||
15707963267949/5000000000000 : (3 7 15 1 292 1 1 1 2 1 3 1 21 17 1 1 1 1 8 1 7 2 1 2 2) |
|||
</pre> |
</pre> |
||