How to resolve the algorithm Mian-Chowla sequence step by step in the C++ programming language
How to resolve the algorithm Mian-Chowla sequence step by step in the C++ 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 C++ programming language
The provided C++ code generates and displays the first 100 terms of the Mian-Chowla sequence. The Mian-Chowla sequence is a sequence of positive integers such that the sum of any two distinct terms is not equal to the value of any other term in the sequence. The code uses two main arrays:
mc
saves the Mian-Chowla sequencesums
saves the sums of available pairs from themc
array TheContains
function is used to check if an item exists in an array. TheMianChowla
function generates the Mian-Chowla sequence. It iterates from 1 ton
to fill themc
array. For each value ofi
, it finds the smallest integerj
such that the sum of any two distinct terms frommc
with indices less than or equal toi
is not equal toj
.- The outer loop iterates from 1 to n, updating the index of the current term being generated in the Mian-Chowla sequence.
- The inner loop iterates through potential values for the current term, starting from the previous term plus 1.
- For each potential value, the code checks if the sum of the current term and any previous term is already in the
sums
array. If it is, the code skips the current value and moves on to the next one. - If the sum is not in the
sums
array, the code adds it and updates the length of thesums
array. - Once a valid value for the current term is found, it is stored in the
mc
array, and the code moves on to the next term. Themain
function calls theMianChowla
function to generate the sequence. It then prints out the first 30 terms and terms 91 to 100 of the sequence, along with the time taken to generate the sequence.
Source code in the cpp programming language
using namespace std;
#include <iostream>
#include <ctime>
#define n 100
#define nn ((n * (n + 1)) >> 1)
bool Contains(int lst[], int item, int size) {
for (int i = 0; i < size; i++) if (item == lst[i]) return true;
return false;
}
int * MianChowla()
{
static int mc[n]; mc[0] = 1;
int sums[nn]; sums[0] = 2;
int sum, le, ss = 1;
for (int i = 1; i < n; i++) {
le = ss;
for (int j = mc[i - 1] + 1; ; j++) {
mc[i] = j;
for (int k = 0; k <= i; k++) {
sum = mc[k] + j;
if (Contains(sums, sum, ss)) {
ss = le; goto nxtJ;
}
sums[ss++] = sum;
}
break;
nxtJ:;
}
}
return mc;
}
int main() {
clock_t st = clock(); int * mc; mc = MianChowla();
double et = ((double)(clock() - st)) / CLOCKS_PER_SEC;
cout << "The first 30 terms of the Mian-Chowla sequence are:\n";
for (int i = 0; i < 30; i++) { cout << mc[i] << ' '; }
cout << "\n\nTerms 91 to 100 of the Mian-Chowla sequence are:\n";
for (int i = 90; i < 100; i++) { cout << mc[i] << ' '; }
cout << "\n\nComputation time was " << et << " seconds.";
}
You may also check:How to resolve the algorithm Totient function step by step in the CLU programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the zkl programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the PureBasic programming language