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

Published on 12 May 2024 09:40 PM
#M4

How to resolve the algorithm Golden ratio/Convergence step by step in the M4 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 M4 programming language

Source code in the m4 programming language

divert(-1)

define(`iterate',`define(`iterations',0)`'_$0(`$1')')
define(`_iterate',
  `define(`iterations',eval(iterations + 1))`'dnl
pushdef(`phi1',eval(10000 + ((10000 * 10000) / ($1))))`'dnl
pushdef(`diff',ifelse(eval((phi1 - ($1)) < 0),1,dnl
eval(($1) - phi1),eval(phi1 - ($1))))`'dnl
ifelse(eval(diff < 1),1,phi1,`_iterate(phi1)')`'dnl
popdef(`phi1',`diff')')

divert`'dnl
eval(iterate(10000) / 10000)`.'eval(iterate(10000) % 10000)
iterations `iterations'

  

You may also check:How to resolve the algorithm Increment a numerical string step by step in the Lingo programming language
You may also check:How to resolve the algorithm Average loop length step by step in the 11l programming language
You may also check:How to resolve the algorithm Average loop length step by step in the Perl programming language
You may also check:How to resolve the algorithm Number names step by step in the APL programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the D programming language