How to resolve the algorithm Farey sequence step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Farey sequence step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

The   Farey sequence   Fn   of order   n   is the sequence of completely reduced fractions between   0   and   1   which, when in lowest terms, have denominators less than or equal to   n,   arranged in order of increasing size. The   Farey sequence   is sometimes incorrectly called a   Farey series.

Each Farey sequence:

The Farey sequences of orders   1   to   5   are:

The length   (the number of fractions)   of a Farey sequence asymptotically approaches:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Farey sequence step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' version 05-04-2017
' compile with: fbc -s console

' TRUE/FALSE are built-in constants since FreeBASIC 1.04
' But we have to define them for older versions.
#Ifndef TRUE
    #Define FALSE 0
    #Define TRUE Not FALSE
#EndIf

Function farey(n As ULong, descending As Long) As ULong

    Dim As Long a, b = 1, c = 1, d = n, k
    Dim As Long aa, bb, cc, dd, count

    If descending = TRUE Then
        a = 1 : c = n -1
    End If

    count += 1
    If n < 12 Then Print Str(a); "/"; Str(b); " ";

    While ((c <= n) And Not descending) Or ((a > 0) And descending)
        aa = a : bb = b : cc = c : dd = d
        k = (n + b) \ d
        a = cc : b = dd : c = k * cc - aa : d = k * dd - bb
        count += 1
        If n < 12 Then Print Str(a); "/"; Str(b); " ";
    Wend

    If n < 12 Then Print

    Return count

End Function

' ------=< MAIN >=------

For i As Long = 1 To 11
    Print "F"; Str(i); " = ";
    farey(i, FALSE)
Next
Print
For i As Long= 100 To 1000 Step 100
    Print "F";Str(i);
    Print iif(i <> 1000, " ", ""); " = ";
    Print Using "######"; farey(i, FALSE)
Next

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Temperature conversion step by step in the AppleScript programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Racket programming language
You may also check:How to resolve the algorithm Infinity step by step in the C++ programming language
You may also check:How to resolve the algorithm Pi step by step in the Sidef programming language