- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Stern-Brocot Sequence

The aim of this article is to implement a program to print the Stern-Brocot sequence.

What is the Stern-Brocot sequence?

Stern-Brocot sequence, which is also known as Stern’s diatomic series, is a sequence of numbers generated as given below.

1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, ...

Although one may find that the Stern-Brocot sequence quite resembles the Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The Stern-Brocot sequence differs from the Fibonacci sequence in the method the Fibonacci sequence is produced. In the Fibonacci series, each number in the series is exactly equal to the total of the two numbers before it. According to the rule that governs the Fibonacci series, each number in the sequence is the sum of two numbers preceding it. For example, 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13 and so on.

Now coming on to the Stern-Brocot sequence, here we not only consider the sum of numbers as in the Fibonacci series but a little more. We have mentioned in detail how the Stern-Brocot sequence is produced below. So in order to get a deeper understanding of the Stern-Brocot sequence, let's dive deep into the article.

## Problem Statement

Implement a program to print the Stern-Brocot sequence.

### Explanation

The sequence's first and second elements are 1:

[1, 1]

Now consider the second element of the sequence.

Add the considered element of the series and its precedent, then attach it to the end of the sequence: (i.e. 1 + 1= 2)

[1, 1, 2]

Put the sequence's considered element at the end of the series:

[1, 1, 2, 1]

Pick the next element of the series (i.e. 2)

Add it to its precedent, then attach it to the end of the sequence: (i.e. 2+1=3)

[1, 1, 2, 1, 3]

Put this considered element at the end of the series:

[1, 1, 2, 1, 3, 2]

The next element to be considered is the 4th element (i.e. 1)

Add it to its precedent, then attach it to the end of the sequence: (i.e. 1+2=3)

[1, 1, 2, 1, 3, 2, 3]

Put this considered element at the end of the series:

[1, 1, 2, 1, 3, 2, 3, 1]

And the series goes like this, the next element to be considered is the 5th element. That is 3.

Therefore, the series goes like 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, ....

### Algorithm

**Step 1**: Start.

**Step 2**: Define n1=1, n2=1, n3, i and number.

**Step 3**: Assign a value to the number.

**Step 4**: Run the loop.

**Step 5**: Add n1 and n2 and assign value to n3.

**Step 6**: Print n3 and n2.

**Step 7**: Update the value of n1 and n2.

**Step 8**: Continue the loop till it stops.

**Step 9**: Stop.

Below is a C program to print the Stern-Brocot sequence is given here

### Example

#include<stdio.h> int main(){ int n1=1, n2=1, n3, i, number=9; printf("Stern-Brocot Sequence:"); printf("\n%d %d",n1,n2);//printing 1 and 1 for(i=2; i<number; i++) { n3=n1+n2; printf(" %d",n3); printf(" %d",n2); n1=n2; n2=n3; } return 0; }

### Output

On execution, it will produce the following output:

Stern-Brocot Sequence: 1 1 2 1 3 2 5 3 8 5 13 8 21 13 34 21

## Conclusion

Likewise, we can print the Stern-Brocot sequence by inputting the limit of elements we want to print.

The challenge of obtaining the Stern-Brocot sequence is resolved in this article. Here C programming code as well as the algorithm to print the Stern-Brocot sequence are provided.