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