La tarea es encontrar el triángulo de números que surge de la conjetura de Gilbreath .
Conjetura de Gilbreath:
Se observa que dada una secuencia de números primos, se puede formar una secuencia por la diferencia absoluta entre el término i -ésimo y (i+1) -ésimo de la secuencia dada y el proceso dado se puede repetir para formar un triángulo de numeros Este número cuando forma los elementos del triángulo de la conjetura de Gilbreath.
El triángulo de Gilbreath se forma de la siguiente manera:
- Tomemos números primos: 2, 3, 5, 7.
- Ahora la diferencia entre primos adyacentes es: 1, 2, 2.
- Ahora la diferencia entre elementos adyacentes es: 1, 0.
- Ahora la diferencia entre elementos adyacentes es: 1.
- De esta forma, el triángulo de Gilbreath se forma como:
2 3 5 7 1 2 2 1 0 1
- Este triángulo se leerá antidiagonalmente hacia arriba como
2, 1, 3, 1, 2, 5, 1, 0, 2, 7,
Ejemplos:
Input: n = 10 Output: 2, 1, 3, 1, 2, 5, 1, 0, 2, 7, Input: n = 15 Output: 2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11
Acercarse:
- El (n, k) ésimo término de la sucesión de Gilbreath viene dado por
- donde n>0 ,
- F(0, k) es el k-ésimo número primo donde n = 0 .
F(n, k) = |F(n – 1, k + 1) – F(n – 1, k)|
- Defina una función recursiva y podemos mapear el (n, k) th término en un mapa y almacenarlos para reducir el cálculo. llenaremos la fila 0 con números primos.
- Atraviese el triángulo de Gilbreath en sentido antidiagonal hacia arriba, así que comenzaremos desde n = 0, k = 0, y en cada paso aumentaremos la k y disminuiremos la n si n<0, entonces asignaremos n=k y k = 0, en este manera podemos atravesar el triángulo anti-diagonalmente hacia arriba.
- Hemos llenado la fila 0 con 100 números primos. si necesitamos encontrar términos más grandes de la serie, podemos aumentar los números primos.
A continuación se muestra la implementación del enfoque anterior:
CPP14
// C++ code for printing the Triangle of numbers // arising from Gilbreath's conjecture #include <bits/stdc++.h> using namespace std; // Check whether the number // is prime or not bool is_Prime(int n) { if (n < 2) return false; for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } // Set the 0th row of the matrix // with c primes from 0, 0 to 0, c-1 void set_primes(map<int, map<int, int> >& mp, map<int, map<int, int> >& hash, int c) { int count = 0; for (int i = 2; count < c; i++) { if (is_Prime(i)) { mp[0][count++] = i; hash[0][count - 1] = 1; } } } // Find the n, k term of matrix of // Gilbreath's conjecture int Gilbreath(map<int, map<int, int> >& mp, map<int, map<int, int> >& hash, int n, int k) { if (hash[n][k] != 0) return mp[n][k]; // recursively find int ans = abs(Gilbreath(mp, hash, n - 1, k + 1) - Gilbreath(mp, hash, n - 1, k)); // store the ans mp[n][k] = ans; return ans; } // Print first n terms of Gilbreath sequence // successive absolute differences of primes // read by antidiagonals upwards. void solve(int n) { int i = 0, j = 0, count = 0; // map to store the matrix // and hash to check if the // element is present or not map<int, map<int, int> > mp, hash; // set the primes of first row set_primes(mp, hash, 100); while (count < n) { // print the Gilbreath number cout << Gilbreath(mp, hash, i, j) << ", "; // increase the count count++; // anti diagonal upwards i--; j++; if (i < 0) { i = j; j = 0; } } } // Driver code int main() { int n = 15; solve(n); return 0; }
Java
// Java code for printing the Triangle of numbers // arising from Gilbreath's conjecture import java.util.*; public class GFG { // Check whether the number // is prime or not static boolean is_Prime(int n) { if (n < 2) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } // Set the 0th row of the matrix // with c primes from 0, 0 to 0, c-1 static void set_primes(HashMap<Integer, HashMap<Integer,Integer>> mp, HashMap<Integer, HashMap<Integer,Integer>> hash, int c) { int count = 0; for (int i = 2; count < c; i++) { if (is_Prime(i)) { if(!mp.containsKey(0)) { mp.put(0, new HashMap<Integer,Integer>()); } if(!mp.get(0).containsKey(count)) { mp.get(0).put(count, i); } else { mp.get(0).put(count, i); } count++; if(!hash.containsKey(0)) { hash.put(0, new HashMap<Integer,Integer>()); } if(!hash.get(0).containsKey(count - 1)) { hash.get(0).put(count - 1, 1); } else { hash.get(0).put(count - 1, 1); } } } } // Find the n, k term of matrix of // Gilbreath's conjecture static int Gilbreath(HashMap<Integer, HashMap<Integer,Integer>> mp, HashMap<Integer, HashMap<Integer,Integer>> hash, int n, int k) { if (hash.containsKey(n) && hash.get(n).containsKey(k) && hash.get(n).get(k) != 0) return mp.get(n).get(k); // recursively find int ans = Math.abs(Gilbreath(mp, hash, n - 1, k + 1) - Gilbreath(mp, hash, n - 1, k)); // store the ans if(!mp.containsKey(n)) { mp.put(n, new HashMap<Integer, Integer>()); } mp.get(n).put(k, ans); return ans; } // Print first n terms of Gilbreath sequence // successive absolute differences of primes // read by antidiagonals upwards. static void solve(int n) { int i = 0, j = 0, count = 0; // map to store the matrix // and hash to check if the // element is present or not HashMap<Integer, HashMap<Integer,Integer>> mp = new HashMap<Integer, HashMap<Integer,Integer>>(); HashMap<Integer, HashMap<Integer,Integer>> hash = new HashMap<Integer, HashMap<Integer,Integer>>(); // set the primes of first row set_primes(mp, hash, 100); while (count < n) { // print the Gilbreath number System.out.print(Gilbreath(mp, hash, i, j) + ", "); // increase the count count++; // anti diagonal upwards i--; j++; if (i < 0) { i = j; j = 0; } } } // Driver code public static void main(String[] args) { int n = 15; solve(n); } } // This code is contributed by divyesh072019.
Python3
# Python3 code for printing the Triangle of numbers # arising from Gilbreath's conjecture import math # Check whether the number # is prime or not def is_Prime(n): if (n < 2): return False; for i in range(2, int(math.sqrt(n)) + 1): if (n % i == 0): return False; return True; # Set the 0th row of the matrix # with c primes from 0, 0 to 0, c-1 def set_primes(mp, hash, c): count = 0; i = 2 while(count < c): if (is_Prime(i)): if 0 not in mp: mp[0] = dict() mp[0][count] = i; count += 1 if 0 not in hash: hash[0] = dict() hash[0][count - 1] = 1; i += 1 # Find the n, k term of matrix of # Gilbreath's conjecture def Gilbreath(mp, hash, n, k): if (n in hash and k in hash[n] and hash[n][k] != 0): return mp[n][k]; # recursively find ans = abs(Gilbreath(mp, hash, n - 1, k + 1) - Gilbreath(mp, hash, n - 1, k)); if n not in mp: mp[n] = dict() # store the ans mp[n][k] = ans; return ans; # Print first n terms of Gilbreath sequence # successive absolute differences of primes # read by antidiagonals upwards. def solve(n): i = 0 j = 0 count = 0; # map to store the matrix # and hash to check if the # element is present or not mp = dict() hash = dict() # set the primes of first row set_primes(mp, hash, 100); while (count < n): # print the Gilbreath number print(Gilbreath(mp, hash, i, j), end = ', ') # increase the count count += 1 # anti diagonal upwards i -= 1 j += 1 if (i < 0): i = j; j = 0; # Driver code if __name__=='__main__': n = 15; solve(n); # This code is contributed by rutvik_56.
C#
// C# code for printing the Triangle of numbers // arising from Gilbreath's conjecture using System; using System.Collections.Generic; class GFG { // Check whether the number // is prime or not static bool is_Prime(int n) { if (n < 2) return false; for (int i = 2; i <= Math.Sqrt(n); i++) if (n % i == 0) return false; return true; } // Set the 0th row of the matrix // with c primes from 0, 0 to 0, c-1 static void set_primes(Dictionary<int, Dictionary<int,int>> mp, Dictionary<int, Dictionary<int,int>> hash, int c) { int count = 0; for (int i = 2; count < c; i++) { if (is_Prime(i)) { if(!mp.ContainsKey(0)) { mp[0] = new Dictionary<int,int>(); } if(!mp[0].ContainsKey(count)) { mp[0].Add(count, i); } else { mp[0][count] = i; } count++; if(!hash.ContainsKey(0)) { hash[0] = new Dictionary<int,int>(); } if(!hash[0].ContainsKey(count - 1)) { hash[0].Add(count - 1, 1); } else { hash[0][count - 1] = 1; } } } } // Find the n, k term of matrix of // Gilbreath's conjecture static int Gilbreath(Dictionary<int, Dictionary<int,int>> mp, Dictionary<int, Dictionary<int,int>> hash, int n, int k) { if (hash.ContainsKey(n) && hash[n].ContainsKey(k) && hash[n][k] != 0) return mp[n][k]; // recursively find int ans = Math.Abs(Gilbreath(mp, hash, n - 1, k + 1) - Gilbreath(mp, hash, n - 1, k)); // store the ans if(!mp.ContainsKey(n)) { mp[n] = new Dictionary<int, int>(); } mp[n][k] = ans; return ans; } // Print first n terms of Gilbreath sequence // successive absolute differences of primes // read by antidiagonals upwards. static void solve(int n) { int i = 0, j = 0, count = 0; // map to store the matrix // and hash to check if the // element is present or not Dictionary<int, Dictionary<int,int>> mp = new Dictionary<int, Dictionary<int,int>>(); Dictionary<int, Dictionary<int,int>> hash = new Dictionary<int, Dictionary<int,int>>(); // set the primes of first row set_primes(mp, hash, 100); while (count < n) { // print the Gilbreath number Console.Write(Gilbreath(mp, hash, i, j) + ", "); // increase the count count++; // anti diagonal upwards i--; j++; if (i < 0) { i = j; j = 0; } } } // Driver code static void Main() { int n = 15; solve(n); } } // This code is contributed by divyeshrabadiya07.
2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,
Referencia : http://oeis.org/A036262
Publicación traducida automáticamente
Artículo escrito por andrew1234 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA