How to resolve the algorithm Mian-Chowla sequence step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mian-Chowla sequence step by step in the Wren programming language

Table of Contents

Problem Statement

The Mian–Chowla sequence is an integer sequence defined recursively.

Mian–Chowla is an infinite instance of a Sidon sequence, and belongs to the class known as B₂ sequences.

The sequence starts with: then for n > 1, an is the smallest positive integer such that every pairwise sum is distinct, for all i and j less than or equal to n.

Demonstrating working through the first few terms longhand: Speculatively try a2 = 2 There are no repeated sums so 2 is the next number in the sequence. Speculatively try a3 = 3 Sum of 4 is repeated so 3 is rejected. Speculatively try a3 = 4 There are no repeated sums so 4 is the next number in the sequence. And so on...

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mian-Chowla sequence step by step in the Wren programming language

Source code in the wren programming language

var mianChowla = Fn.new { |n|
    var mc = List.filled(n, 0)
    var sums = {}
    var ts = {}
    mc[0] = 1
    sums[2] = true
    for (i in 1...n) {
        var j = mc[i-1] + 1
        while (true) {
            mc[i] = j
            for (k in 0..i) {
                var sum = mc[k] + j
                if (sums[sum]) {
                    ts.clear()
                    break
                }
                ts[sum] = true
            }
            if (ts.count > 0) {
                for (key in ts.keys) sums[key] = true
                break
            }
            j = j + 1
        }
    }
    return mc
}

var start = System.clock
var mc = mianChowla.call(100)
System.print("The first 30 terms of the Mian-Chowla sequence are:\n%(mc[0..29].join(" "))")
System.print("\nTerms 91 to 100 of the Mian-Chowla sequence are:\n%(mc[90..99].join(" "))")
System.print("\nTook %(((System.clock - start)*1000).round) milliseconds")

  

You may also check:How to resolve the algorithm File input/output step by step in the Tcl programming language
You may also check:How to resolve the algorithm Fork step by step in the C programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Matlab programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Maple programming language