# Stern-Brocot Sequence

Stern Brocot sequence is similar to Fibonacci sequence but it is different in the way fibonacci sequence is generated .
Generation of Stern Brocot sequence :

1. First and second element of the sequence is 1 and 1.

2. Consider the second member of the sequence . Then, sum the considered member of the sequence and it’s precedent i.e (1 + 1 = 2) . Now 2 is the next element of our series . Sequence will be [ 1, 1, 2 ]

3. After this element, our next element of the sequence will be the considered element of our second step. Now the sequence will be [ 1, 1, 2, 1 ]

4. Again we do the step 2, but now the considered element will be 2(3rd element ). So, next number of sequence will be sum of considered number and it’s precedent (2 + 1 = 3). Sequence will be now [ 1, 1, 2, 1, 3 ]

5. Like step 3, the next element will be the considered element i.e 2 . Thus sequence will be [ 1, 1, 2, 1, 3, 2 ]

6. Hence this process continues, now next considered element will be 1( 4th element ) .

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Here is the simple program to print Stern Brocot sequence .

## C++

 `// CPP program to print Brocot Sequence ` `#include ` `using` `namespace` `std; ` ` `  `void` `SternSequenceFunc(vector<``int``>& BrocotSequence, ``int` `n) ` `{ ` `    ``// loop to create sequence ` `    ``for` `(``int` `i = 1; BrocotSequence.size() < n; i++) ` `    ``{ ` `        ``int` `considered_element = BrocotSequence[i]; ` `        ``int` `precedent = BrocotSequence[i - 1]; ` ` `  `        ``// adding sum of considered element and it's precedent ` `        ``BrocotSequence.push_back(considered_element + precedent); ` `         `  `        ``// adding next considered element ` `        ``BrocotSequence.push_back(considered_element); ` `    ``} ` ` `  `    ``// printing sequence.. ` `    ``for` `(``int` `i = 0; i < 15; ++i) ` `        ``cout << BrocotSequence[i] << ``" "``; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `n = 15; ` `    ``vector<``int``> BrocotSequence; ` `     `  `    ``// adding first two element ` `    ``// in the sequence ` `    ``BrocotSequence.push_back(1); ` `    ``BrocotSequence.push_back(1); ` `     `  `    ``SternSequenceFunc(BrocotSequence, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print  ` `// Brocot Sequence ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` `     `  `static` `void` `SternSequenceFunc(Vector ` `                         ``BrocotSequence, ``int` `n) ` `{ ` `    ``// loop to create sequence ` `    ``for` `(``int` `i = ``1``; BrocotSequence.size() < n; i++) ` `    ``{ ` `        ``int` `considered_element = BrocotSequence.get(i); ` `        ``int` `precedent = BrocotSequence.get(i-``1``); ` ` `  `        ``// adding sum of considered element and it's precedent ` `        ``BrocotSequence.add(considered_element + precedent); ` `         `  `        ``// adding next considered element ` `        ``BrocotSequence.add(considered_element); ` `    ``} ` ` `  `    ``// printing sequence.. ` `    ``for` `(``int` `i = ``0``; i < ``15``; ++i) ` `        ``System.out.print(BrocotSequence.get(i) + ``" "``); ` `} ` `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) { ` `         `  `        ``int` `n = ``15``; ` `        ``Vector BrocotSequence = ``new` `Vector(); ` `         `  `        ``// adding first two element ` `        ``// in the sequence ` `        ``BrocotSequence.add(``1``); ` `        ``BrocotSequence.add(``1``); ` `         `  `        ``SternSequenceFunc(BrocotSequence, n); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by Gitanjali. `

## Python3

 `# Python program to print  ` `# Brocot Sequence ` `import` `math ` `  `  `def` `SternSequenceFunc(BrocotSequence, n): ` `     `  `    ``# loop to create sequence ` `    ``for` `i ``in` `range``(``1``,  n): ` `         `  `        ``considered_element ``=` `BrocotSequence[i] ` `        ``precedent ``=` `BrocotSequence[i``-``1``] ` `  `  `        ``# adding sum of considered ` `        ``# element and it's precedent ` `        ``BrocotSequence.append(considered_element ``+` `precedent) ` `          `  `        ``# adding next considered element ` `        ``BrocotSequence.append(considered_element) ` `     `  `  `  `    ``# printing sequence.. ` `    ``for` `i ``in` `range``(``0``, ``15``): ` `        ``print``(BrocotSequence[i] , end``=``" "``) ` ` `  `# Driver code ` `n ``=` `15` `BrocotSequence ``=` `[] ` ` `  `# adding first two element ` `# in the sequence ` `BrocotSequence.append(``1``) ` `BrocotSequence.append(``1``) ` `          `  `SternSequenceFunc(BrocotSequence, n) ` ` `  `# This code is contributed by Gitanjali. `

## C#

 `// C# program to print  ` `// Brocot Sequence ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` `static` `void` `SternSequenceFunc(List<``int``> ` `                 ``BrocotSequence, ``int` `n) ` `{ ` `    ``// loop to create sequence ` `    ``for` `(``int` `i = 1;  ` `             ``BrocotSequence.Count < n; i++) ` `    ``{ ` `        ``int` `considered_element =  ` `                       ``BrocotSequence[i]; ` `        ``int` `precedent =  ` `                   ``BrocotSequence[i - 1]; ` ` `  `        ``// adding sum of considered ` `        ``// element and it's precedent ` `        ``BrocotSequence.Add(considered_element +  ` `                                    ``precedent); ` `         `  `        ``// adding next  ` `        ``// considered element ` `        ``BrocotSequence.Add( ` `                       ``considered_element); ` `    ``} ` ` `  `    ``// printing sequence.. ` `    ``for` `(``int` `i = 0; i < 15; ++i) ` `        ``Console.Write( ` `                ``BrocotSequence[i] + ``" "``); ` `} ` `// Driver code ` `static` `void` `Main ()  ` `{ ` `     `  `    ``int` `n = 15; ` `    ``List<``int``> BrocotSequence =  ` `                    ``new` `List<``int``>(); ` `     `  `    ``// adding first two element ` `    ``// in the sequence ` `    ``BrocotSequence.Add(1); ` `    ``BrocotSequence.Add(1); ` `     `  `    ``SternSequenceFunc(BrocotSequence, n); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

## PHP

 ` `

Output:

```1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
```

References :
Github

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : manishshaw1

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.