Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Baum Sweet Sequence in C Program?
The Baum Sweet Sequence is a binary sequence where each term is determined by analyzing the binary representation of a number. If a number n has any block of consecutive zeros with odd length in its binary form, then the nth term is 0, otherwise it is 1.
For example, if the number is 4, its binary representation is 100. This has one block of two consecutive zeros (even length), so the 4th term of the Baum Sweet sequence is 1.
Syntax
int baumSweetTerm(int n);
Algorithm
The algorithm works as follows −
BaumSweetSeqTerm(n):
1. Convert n to binary representation
2. Initialize result as 1
3. Scan through binary digits
4. For each block of consecutive zeros:
- Count the length
- If length is odd, set result = 0
5. Return result
Example 1: Basic Implementation
This example demonstrates the basic implementation of finding the nth term of Baum Sweet sequence −
#include <stdio.h>
int baumSweetTerm(int n) {
if (n == 0) return 1;
int baum = 1;
int temp = n;
while (temp > 0) {
if ((temp & 1) == 0) { // Found a zero bit
int count = 0;
// Count consecutive zeros
while (temp > 0 && (temp & 1) == 0) {
count++;
temp >>= 1;
}
// If odd number of consecutive zeros, result is 0
if (count % 2 == 1) {
baum = 0;
break;
}
} else {
temp >>= 1;
}
}
return baum;
}
int main() {
int n = 4;
printf("Binary of %d: ", n);
// Display binary representation
for (int i = 7; i >= 0; i--) {
printf("%d", (n >> i) & 1);
}
printf("<br>");
printf("Baum Sweet term for %d: %d<br>", n, baumSweetTerm(n));
return 0;
}
Binary of 4: 00000100 Baum Sweet term for 4: 1
Example 2: Multiple Terms
This example calculates the first few terms of the Baum Sweet sequence −
#include <stdio.h>
int baumSweetTerm(int n) {
if (n == 0) return 1;
int baum = 1;
int temp = n;
while (temp > 0) {
if ((temp & 1) == 0) {
int count = 0;
while (temp > 0 && (temp & 1) == 0) {
count++;
temp >>= 1;
}
if (count % 2 == 1) {
baum = 0;
break;
}
} else {
temp >>= 1;
}
}
return baum;
}
int main() {
printf("First 16 terms of Baum Sweet sequence:<br>");
for (int i = 0; i < 16; i++) {
printf("n=%2d, binary=", i);
for (int j = 7; j >= 0; j--) {
printf("%d", (i >> j) & 1);
}
printf(", term=%d<br>", baumSweetTerm(i));
}
return 0;
}
First 16 terms of Baum Sweet sequence: n= 0, binary=00000000, term=1 n= 1, binary=00000001, term=1 n= 2, binary=00000010, term=0 n= 3, binary=00000011, term=1 n= 4, binary=00000100, term=1 n= 5, binary=00000101, term=1 n= 6, binary=00000110, term=1 n= 7, binary=00000111, term=1 n= 8, binary=00001000, term=0 n= 9, binary=00001001, term=0 n=10, binary=00001010, term=0 n=11, binary=00001011, term=0 n=12, binary=00001100, term=1 n=13, binary=00001101, term=1 n=14, binary=00001110, term=1 n=15, binary=00001111, term=1
How It Works
The algorithm examines the binary representation bit by bit. When it encounters a zero bit, it counts how many consecutive zeros follow. If any group of consecutive zeros has an odd count, the term becomes 0. Otherwise, it remains 1.
Key Points
- The sequence starts with index 0, and the 0th term is defined as 1
- Only consecutive blocks of zeros matter − isolated ones or groups separated by ones are counted separately
- The algorithm uses bitwise operations for efficient binary digit extraction
- Time complexity is O(log n) where n is the input number
Conclusion
The Baum Sweet sequence provides an interesting way to classify numbers based on their binary representations. Understanding this sequence helps in exploring properties of binary numbers and bitwise operations in C programming.
