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

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Mian-Chowla sequence step by step in the Go 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 Go programming language

The provided Go code implements the Mian-Chowla sequence, a sequence of positive integers with several interesting properties. The code includes two versions of the mianChowla function, which generates the sequence up to a specified length.

Version 1:

func mianChowla(n int) []int {
   mc := make([]int, n)
   mc[0] = 1
   is := []int{2}
   var sum int
   for i := 1; i < n; i++ {
       le := len(is)
   jloop:
       for j := mc[i-1] + 1; ; j++ {
           mc[i] = j
           for k := 0; k <= i; k++ {
               sum = mc[k] + j
               if contains(is, sum) {
                   is = is[0:le]
                   continue jloop
               }
               is = append(is, sum)
           }
           break
       }
   }
   return mc
}

Explanation:

  1. It initializes a slice mc to hold the Mian-Chowla sequence up to length n. The first element mc[0] is set to 1.
  2. An integer array is is initialized with the value 2. This array will be used to check for the presence of sums of previous elements.
  3. A variable sum is used to store the sum of two elements from mc.
  4. It enters a loop to generate the sequence:
    • For each index i from 1 to n-1, it starts searching for a value j that satisfies certain conditions.
    • It initializes le with the current length of the is array to detect changes in the array during the loop.
    • It enters a nested loop (labeled jloop) to find a suitable value for j.
    • Inside the nested loop, it tries different values of j starting from mc[i-1] + 1.
    • It checks if the sum of any of the previous elements mc[k] and j is already present in the is array. If so, it resets is to its initial values and continues searching for j.
    • If no sum conflicts are found, it updates mc[i] with j, adds the sum to the is array, and breaks out of the nested loop.

Version 2:

func mianChowla(n int) []int {
   mc := make([]int, n)
   mc[0] = 1
   is := make(set, n*(n+1)/2)
   is[2] = true
   var sum int
   isx := make([]int, 0, n)
   for i := 1; i < n; i++ {
       isx = isx[:0]
   jloop:
       for j := mc[i-1] + 1; ; j++ {
           mc[i] = j
           for k := 0; k <= i; k++ {
               sum = mc[k] + j
               if is[sum] {                   
                   isx = isx[:0]
                   continue jloop
               }
               isx = append(isx, sum)
           }
           for _, x := range isx {
               is[x] = true
           }
           break
       }
   }
   return mc
}

Explanation:

  1. It follows the same general structure as Version 1.
  2. The main difference is in the way it handles the is array. Instead of using an integer array, it uses a set of type map[int]bool. A set is an unordered collection of unique elements.
  3. It uses a slice isx to temporarily store the sums of previous elements and add them to the is set in one go after finding a valid j.
  4. The isx slice is cleared before each iteration of the jloop.

Main Function:

The main function calls the mianChowla function with n=100 to generate the first 100 terms of the Mian-Chowla sequence. It then prints the first 30 and terms from 91 to 100 of the sequence.

Source code in the go programming language

package main

import "fmt"

func contains(is []int, s int) bool {
    for _, i := range is {
        if s == i {
            return true
        }
    }
    return false
}

func mianChowla(n int) []int {
    mc := make([]int, n)
    mc[0] = 1
    is := []int{2}
    var sum int
    for i := 1; i < n; i++ {
        le := len(is)
    jloop:
        for j := mc[i-1] + 1; ; j++ {
            mc[i] = j
            for k := 0; k <= i; k++ {
                sum = mc[k] + j
                if contains(is, sum) {
                    is = is[0:le]
                    continue jloop
                }
                is = append(is, sum)
            }
            break
        }
    }
    return mc
}

func main() {
    mc := mianChowla(100)
    fmt.Println("The first 30 terms of the Mian-Chowla sequence are:")
    fmt.Println(mc[0:30])
    fmt.Println("\nTerms 91 to 100 of the Mian-Chowla sequence are:")
    fmt.Println(mc[90:100])
}


package main

import "fmt"

type set map[int]bool

func mianChowla(n int) []int {
    mc := make([]int, n)
    mc[0] = 1
    is := make(set, n*(n+1)/2)
    is[2] = true
    var sum int
    isx := make([]int, 0, n)
    for i := 1; i < n; i++ {
        isx = isx[:0]
    jloop:
        for j := mc[i-1] + 1; ; j++ {
            mc[i] = j
            for k := 0; k <= i; k++ {
                sum = mc[k] + j
                if is[sum] {                   
                    isx = isx[:0]
                    continue jloop
                }
                isx = append(isx, sum)
            }
            for _, x := range isx {
                is[x] = true
            }
            break
        }
    }
    return mc
}

func main() {
    mc := mianChowla(100)
    fmt.Println("The first 30 terms of the Mian-Chowla sequence are:")
    fmt.Println(mc[0:30])
    fmt.Println("\nTerms 91 to 100 of the Mian-Chowla sequence are:")
    fmt.Println(mc[90:100])
}


  

You may also check:How to resolve the algorithm RIPEMD-160 step by step in the zkl programming language
You may also check:How to resolve the algorithm Create a file step by step in the AWK programming language
You may also check:How to resolve the algorithm Distributed programming step by step in the Nim programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the D programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the REXX programming language