How to resolve the algorithm Mian-Chowla sequence step by step in the Go programming language
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:
- It initializes a slice
mc
to hold the Mian-Chowla sequence up to lengthn
. The first elementmc[0]
is set to 1. - 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. - A variable
sum
is used to store the sum of two elements frommc
. - It enters a loop to generate the sequence:
- For each index
i
from 1 ton-1
, it starts searching for a valuej
that satisfies certain conditions. - It initializes
le
with the current length of theis
array to detect changes in the array during the loop. - It enters a nested loop (labeled
jloop
) to find a suitable value forj
. - Inside the nested loop, it tries different values of
j
starting frommc[i-1] + 1
. - It checks if the sum of any of the previous elements
mc[k]
andj
is already present in theis
array. If so, it resetsis
to its initial values and continues searching forj
. - If no sum conflicts are found, it updates
mc[i]
withj
, adds the sum to theis
array, and breaks out of the nested loop.
- For each index
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:
- It follows the same general structure as Version 1.
- The main difference is in the way it handles the
is
array. Instead of using an integer array, it uses aset
of typemap[int]bool
. A set is an unordered collection of unique elements. - It uses a slice
isx
to temporarily store the sums of previous elements and add them to theis
set in one go after finding a validj
. - The
isx
slice is cleared before each iteration of thejloop
.
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