How to resolve the algorithm Continued fraction step by step in the Scheme programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction step by step in the Scheme programming language

Table of Contents

Problem Statement

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use

a

0

= 1

{\displaystyle a_{0}=1}

then

a

N

= 2

{\displaystyle a_{N}=2}

.

b

N

{\displaystyle b_{N}}

is always

1

{\displaystyle 1}

. For Napier's Constant, use

a

0

= 2

{\displaystyle a_{0}=2}

, then

a

N

= N

{\displaystyle a_{N}=N}

.

b

1

= 1

{\displaystyle b_{1}=1}

then

b

N

= N − 1

{\displaystyle b_{N}=N-1}

. For Pi, use

a

0

= 3

{\displaystyle a_{0}=3}

then

a

N

= 6

{\displaystyle a_{N}=6}

.

b

N

= ( 2 N − 1

)

2

{\displaystyle b_{N}=(2N-1)^{2}}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction step by step in the Scheme programming language

Source code in the scheme programming language

#!r6rs
(import (rnrs base (6))
        (srfi :41 streams))

(define nats (stream-cons 0 (stream-map (lambda (x) (+ x 1)) nats)))

(define (build-stream fn) (stream-map fn nats))

(define (stream-cycle s . S)
  (cond
    ((stream-null? (car S)) stream-null)
    (else (stream-cons (stream-car s)
                       (apply stream-cycle (append S (list (stream-cdr s))))))))

(define (cf-floor cf) (stream-car cf))
(define (cf-num cf) (stream-car (stream-cdr cf)))
(define (cf-denom cf) (stream-cdr (stream-cdr cf)))

(define (cf-integer? x) (stream-null? (stream-cdr x)))

(define (cf->real x)
  (let refine ((x x) (n 65536))
    (cond
      ((= n 0) +inf.0)
      ((cf-integer? x) (cf-floor x))
      (else (+ (cf-floor x)
               (/ (cf-num x)
                  (refine (cf-denom x) (- n 1))))))))

(define (real->cf x)
  (let-values (((integer-part fractional-part) (div-and-mod x 1)))
    (if (= fractional-part 0.0)
        (stream (exact integer-part))
        (stream-cons
         (exact integer-part)
         (stream-cons
          1
          (real->cf (/ fractional-part)))))))


(define sqrt2 (stream-cons 1 (stream-constant 1 2)))

(define napier
  (stream-append (stream 2 1)
                 (stream-cycle (stream-cdr nats) (stream-cdr nats))))

(define pi
  (stream-cons 3
               (stream-cycle (build-stream (lambda (n) (expt (- (* 2 (+ n 1)) 1) 2)))
                             (stream-constant 6))))


> (cf->real sqrt2)
1.4142135623730951
> (cf->real napier)
2.7182818284590455
> (cf->real pi)
3.141592653589794


  

You may also check:How to resolve the algorithm Reverse words in a string step by step in the Phix programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Oz programming language
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Elena programming language
You may also check:How to resolve the algorithm Set right-adjacent bits step by step in the Python programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Nim programming language