How to resolve the algorithm Golden ratio/Convergence step by step in the COBOL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Golden ratio/Convergence step by step in the COBOL programming language

Table of Contents

Problem Statement

The golden ratio can be defined as the continued fraction Thus

ϕ

1 +

1

/

ϕ

{\displaystyle \phi =1+{1/\phi }}

. Multiplying both sides by

ϕ

{\displaystyle \phi }

and solving the resulting quadratic equation for its positive solution, one gets

ϕ

( 1 +

5

)

/

2 ≈ 1.61803398875

{\displaystyle \phi =(1+{\sqrt {5}})/2\approx 1.61803398875}

. The golden ratio has the slowest convergence of any continued fraction, as one might guess by noting that the denominators are made of the smallest positive integer. This task treats the problem of convergence in a somewhat backwards fashion: we are going to iterate the recursion

ϕ

n + 1

= 1 +

1

/

ϕ

n

{\displaystyle \phi _{n+1}=1+{1/\phi _{n}}}

,

ϕ

0

= 1

{\displaystyle \phi _{0}=1}

, and see how long it takes to get an answer.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Golden ratio/Convergence step by step in the COBOL programming language

Source code in the cobol programming language

       *> -*- mode: cobol -*-  
       IDENTIFICATION DIVISION.
       PROGRAM-ID. GOLDEN_RATIO_CONVERGENCE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 PHI0 PIC 9(1)V9(12).
       01 PHI1 PIC 9(1)V9(12).
       01 DIFF PIC S9(1)V9(12).
       01 ERR PIC S9(1)V9(12).
       01 NOMINAL-VALUE PIC 9(1)V9(12).
       01 ITER PIC 9(2).

       PROCEDURE DIVISION.
           MOVE 0 TO ITER
           MOVE 1.0 TO PHI0.
       LOOP.
           COMPUTE PHI1 = 1.0 + (1.0 / PHI0)
           COMPUTE DIFF = FUNCTION ABS (PHI1 - PHI0)
           MOVE PHI1 TO PHI0
           ADD 1 TO ITER
           IF 100000 * DIFF > 1.0
               GO TO LOOP
           END-IF
           DISPLAY 'RESULT: ' PHI1 ' AFTER ' ITER ' ITERATIONS'
           MOVE 1.61803398874989 TO NOMINAL-VALUE
           COMPUTE ERR = PHI1 - NOMINAL-VALUE
           DISPLAY 'THE ERROR IS APPROXIMATELY ' ERR.


  

You may also check:How to resolve the algorithm Square but not cube step by step in the Rust programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Raku programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the BASIC programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Commodore BASIC programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Free Pascal programming language