La secuencia de Stern Brocot es similar a la secuencia de Fibonacci pero es diferente en la forma en que se genera la secuencia de Fibonacci.
Generación de la secuencia Stern Brocot:
1. El primer y segundo elemento de la secuencia son 1 y 1.
2. Considere el segundo miembro de la secuencia. Luego, suma el miembro considerado de la secuencia y su precedente, es decir (1 + 1 = 2). Ahora 2 es el siguiente elemento de nuestra serie. La secuencia será [ 1, 1, 2 ]
3. Después de este elemento, nuestro próximo elemento de la secuencia será el elemento considerado de nuestro segundo paso. Ahora la secuencia será [ 1, 1, 2, 1 ]
4. Nuevamente hacemos el paso 2, pero ahora el elemento considerado será el 2 (3er elemento). Entonces, el próximo número de secuencia será la suma del número considerado y su precedente (2 + 1 = 3). La secuencia será ahora [1, 1, 2, 1, 3]
5. Al igual que el paso 3, el siguiente elemento será el elemento considerado, es decir, 2. Por lo tanto, la secuencia será [ 1, 1, 2, 1, 3, 2 ]
6. Por lo tanto, este proceso continúa, ahora el siguiente elemento considerado será 1 (4º elemento).
Aquí está el programa simple para imprimir la secuencia Stern Brocot.
C++
// CPP program to print Brocot Sequence #include <bits/stdc++.h> 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<Integer> 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<Integer> BrocotSequence = new Vector<Integer>(); // 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
<?php // PHP program to // print Brocot Sequence function SternSequenceFunc(&$BrocotSequence, $n) { // loop to create sequence for ($i = 1; count($BrocotSequence) < $n; $i++) { $considered_element = $BrocotSequence[$i]; $precedent = $BrocotSequence[$i - 1]; // adding sum of considered // element and it's precedent array_push($BrocotSequence, $considered_element + $precedent); // adding next // considered element array_push($BrocotSequence, $considered_element); } // printing sequence.. for ($i = 0; $i < 15; ++$i) echo ($BrocotSequence[$i]." "); } // Driver code $n = 15; $BrocotSequence = array(); // adding first two element // in the sequence array_push($BrocotSequence, 1); array_push($BrocotSequence, 1); SternSequenceFunc($BrocotSequence, $n); // This code is contributed by // Manish Shaw(manishshaw1) ?>
Javascript
<script> // Javascript program to print Brocot Sequence function SternSequenceFunc( BrocotSequence, n) { // loop to create sequence for (var i = 1; BrocotSequence.length < n; i++) { var considered_element = BrocotSequence[i]; var precedent = BrocotSequence[i - 1]; // adding sum of considered element and it's precedent BrocotSequence.push(considered_element + precedent); // adding next considered element BrocotSequence.push(considered_element); } // printing sequence.. for (var i = 0; i < 15; ++i) document.write( BrocotSequence[i] + " "); } var n = 15; var BrocotSequence = []; // adding first two element // in the sequence BrocotSequence.push(1); BrocotSequence.push(1); SternSequenceFunc(BrocotSequence, n); // This code is contributed by rrrtnx. </script>
Producción:
1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
Referencias:
Github
Publicación traducida automáticamente
Artículo escrito por Surya Priy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA