How to resolve the algorithm Metallic ratios step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Metallic ratios step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

Many people have heard of the Golden ratio, phi (φ). Phi is just one of a series of related ratios that are referred to as the "Metallic ratios". The Golden ratio was discovered and named by ancient civilizations as it was thought to be the most pure and beautiful (like Gold). The Silver ratio was was also known to the early Greeks, though was not named so until later as a nod to the Golden ratio to which it is closely related. The series has been extended to encompass all of the related ratios and was given the general name Metallic ratios (or Metallic means). Somewhat incongruously as the original Golden ratio referred to the adjective "golden" rather than the metal "gold". Metallic ratios are the real roots of the general form equation: where the integer b determines which specific one it is. Using the quadratic equation: Substitute in (from the top equation) 1 for a, -1 for c, and recognising that -b is negated we get: We only want the real root: When we set b to 1, we get an irrational number: the Golden ratio. With b set to 2, we get a different irrational number: the Silver ratio. When the ratio b is 3, it is commonly referred to as the Bronze ratio, 4 and 5 are sometimes called the Copper and Nickel ratios, though they aren't as standard. After that there isn't really any attempt at standardized names. They are given names here on this page, but consider the names fanciful rather than canonical. Note that technically, b can be 0 for a "smaller" ratio than the Golden ratio. We will refer to it here as the Platinum ratio, though it is kind-of a degenerate case. Metallic ratios where b > 0 are also defined by the irrational continued fractions:

So, The first ten Metallic ratios are:

There are other ways to find the Metallic ratios; one, (the focus of this task) is through successive approximations of Lucas sequences. A traditional Lucas sequence is of the form: and starts with the first 2 values 0, 1. For our purposes in this task, to find the metallic ratios we'll use the form: ( P is set to b and Q is set to -1. ) To avoid "divide by zero" issues we'll start the sequence with the first two terms 1, 1. The initial starting value has very little effect on the final ratio or convergence rate. Perhaps it would be more accurate to call it a Lucas-like sequence. At any rate, when b = 1 we get: more commonly known as the Fibonacci sequence. When b = 2:

And so on.

To find the ratio by successive approximations, divide the (n+1)th term by the nth. As n grows larger, the ratio will approach the b metallic ratio. For b = 1 (Fibonacci sequence): It converges, but pretty slowly. In fact, the Golden ratio has the slowest possible convergence for any irrational number.

For each of the first 10 Metallic ratios; b = 0 through 9: Optional, stretch goal - Show the value and number of iterations n, to approximate the Golden ratio to 256 decimal places. You may assume that the approximation has been reached when the next iteration does not cause the value (to the desired places) to change.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Metallic ratios step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Imports BI = System.Numerics.BigInteger

Module Module1

    Function IntSqRoot(v As BI, res As BI) As BI
        REM res is the initial guess
        Dim term As BI = 0
        Dim d As BI = 0
        Dim dl As BI = 1
        While dl <> d
            term = v / res
            res = (res + term) >> 1
            dl = d
            d = term - res
        End While
        Return term
    End Function

    Function DoOne(b As Integer, digs As Integer) As String
        REM calculates result via square root, not iterations
        Dim s = b * b + 4
        digs += 1
        Dim g As BI = Math.Sqrt(s * Math.Pow(10, digs))
        Dim bs = IntSqRoot(s * BI.Parse("1" + New String("0", digs << 1)), g)
        bs += b * BI.Parse("1" + New String("0", digs))
        bs >>= 1
        bs += 4
        Dim st = bs.ToString
        digs -= 1
        Return String.Format("{0}.{1}", st(0), st.Substring(1, digs))
    End Function

    Function DivIt(a As BI, b As BI, digs As Integer) As String
        REM performs division
        Dim al = a.ToString.Length
        Dim bl = b.ToString.Length
        digs += 1
        a *= BI.Pow(10, digs << 1)
        b *= BI.Pow(10, digs)
        Dim s = (a / b + 5).ToString
        digs -= 1
        Return s(0) + "." + s.Substring(1, digs)
    End Function

    REM custom formatting
    Function Joined(x() As BI) As String
        Dim wids() = {1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
        Dim res = ""
        For i = 0 To x.Length - 1
            res += String.Format("{0," + (-wids(i)).ToString + "} ", x(i))
        Next
        Return res
    End Function

    Sub Main()
        REM calculates and checks each "metal"
        Console.WriteLine("Metal B Sq.Rt Iters /---- 32 decimal place value ----\\  Matches Sq.Rt Calc")
        Dim t = ""
        Dim n As BI
        Dim nm1 As BI
        Dim k As Integer
        Dim j As Integer
        For b = 0 To 9
            Dim lst(14) As BI
            lst(0) = 1
            lst(1) = 1
            For i = 2 To 14
                lst(i) = b * lst(i - 1) + lst(i - 2)
            Next
            REM since all the iterations (except Pt) are > 15, continue iterating from the end of the list of 15
            n = lst(14)
            nm1 = lst(13)
            k = 0
            j = 13
            While k = 0
                Dim lt = t
                t = DivIt(n, nm1, 32)
                If lt = t Then
                    k = If(b = 0, 1, j)
                End If
                Dim onn = n
                n = b * n + nm1
                nm1 = onn

                j += 1
            End While
            Console.WriteLine("{0,4}  {1}   {2,2}    {3, 2}  {4}  {5}" + vbNewLine + "{6,19} {7}", "Pt Au Ag CuSn Cu Ni Al Fe Sn Pb".Split(" ")(b), b, b * b + 4, k, t, t = DoOne(b, 32), "", Joined(lst))
        Next
        REM now calculate and check big one
        n = 1
        nm1 = 1
        k = 0
        j = 1
        While k = 0
            Dim lt = t
            t = DivIt(n, nm1, 256)
            If lt = t Then
                k = j
            End If
            Dim onn = n
            n += nm1
            nm1 = onn

            j += 1
        End While
        Console.WriteLine()
        Console.WriteLine("Au to 256 digits:")
        Console.WriteLine(t)
        Console.WriteLine("Iteration count: {0}  Matched Sq.Rt Calc: {1}", k, t = DoOne(1, 256))
    End Sub

End Module


  

You may also check:How to resolve the algorithm Loops/While step by step in the Panoramic programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Lean programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Batch File programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the REXX programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the VTL-2 programming language