How to resolve the algorithm Fusc sequence step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fusc sequence step by step in the Wren programming language

Table of Contents

Problem Statement

The   fusc   integer sequence is defined as:

Note that MathWorld's definition starts with unity, not zero.   This task will be using the OEIS' version   (above).

where   A   is some non-negative integer expressed in binary,   and where   B   is the binary value of   A   reversed.

Fusc numbers are also known as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fusc sequence step by step in the Wren programming language

Source code in the wren programming language

import "./fmt" for Fmt

System.print("The first 61 numbers in the fusc sequence are:")
var fusc = [0, 1]
var fusc2 = [[0, 0]]
var maxLen = 1
var n = 2
while (n < 20e6) { // limit to indices under 20 million say
    var f  = (n % 2  == 0) ? fusc[n/2] : fusc[(n-1)/2] + fusc[(n+1)/2]
    fusc.add(f)
    var len = "%(f)".count
    if (len > maxLen) {
        maxLen = len
        if (n <= 60) {
            fusc2.add([n, f])
        } else {
            System.print("%(Fmt.dc(10, n))  %(Fmt.dc(0, f))")
        }
    }
    if (n == 60 ) {
        for (f in fusc) System.write("%(f) ")
        System.print("\n\nFirst terms longer than any previous ones for indices < 20,000,000:")
        System.print("     Index  Value")
        for (iv in fusc2) System.print("%(Fmt.d(10, iv[0]))  %(iv[1])")
    }
    n = n + 1
}


  

You may also check:How to resolve the algorithm Tarjan step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Logistic curve fitting in epidemiology step by step in the Arturo programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Parsing/RPN to infix conversion step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the PureBasic programming language