Triangle of numbers arising from Gilbreath’s conjecture
The task is to find the triangle of numbers arising from Gilbreath’s conjecture.
Gilbreath’s conjecture:
It is observed that given a sequence of prime numbers, a sequence can be formed by the absolute difference between the ith and (i+1)th term of the given sequence and the given process can be repeated to form a triangle of numbers. This numbers when forms the elements of Gilbreath conjecture triangle.
The Gilbreath triangle is formed as follows:
- Let us take primes: 2, 3, 5, 7.
- Now the difference between adjacent primes is: 1, 2, 2.
- Now the difference between adjacent elements is: 1, 0.
- Now the difference between adjacent elements is: 1.
- In this way, the Gilbreath triangle is formed as:
2 3 5 7 1 2 2 1 0 1
- This triangle will be read anti-diagonally upwards as
2, 1, 3, 1, 2, 5, 1, 0, 2, 7,
Examples:
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
Approach:
- The (n, k) th term of the Gilbreath sequence is given by
- where n>0,
- F(0, k) is the kth prime number where n = 0. F(n, k) = |F(n – 1, k + 1) – F(n – 1, k)|
- Define a recursive function and we can map the (n, k)th term in a map and store them to reduce computation. we will fill the 0th row with primes.
- Traverse the Gilbreath triangle anti-diagonally upwards so we will start from n = 0, k = 0, and in each step increase the k and decrease the n if n<0 then we will assign n=k and k = 0, in this way we can traverse the triangle anti-diagonally upwards.
- We have filled the 0th row with 100 primes. if we need to find larger terms of the series we can increase the primes.
Below is the implementation of the above approach:
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. |
Javascript
// JS code for printing the Triangle of numbers // arising from Gilbreath's conjecture // Check whether the number // is prime or not function is_Prime(n) { if (n < 2) return false ; for (let 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 function set_primes(mp, hash, c) { let count = 0; for (let i = 2; count < c; i++) { if (is_Prime(i)) { if (!mp.hasOwnProperty(0)) { mp[0] = {}; } if (!mp[0].hasOwnProperty(count)) { mp[0][count] = i; } else { mp[0][count] = i; } count++; if (!hash.hasOwnProperty(0)) { hash[0] = {}; } if (!hash[0].hasOwnProperty(count - 1)) { hash[0][count - 1] = 1; } else { hash[0][count - 1] = 1; } } } } // Find the n, k term of matrix of // Gilbreath's conjecture function Gilbreath(mp, hash, n, k) { if (hash.hasOwnProperty(n)) if (hash[n].hasOwnProperty(k)) return mp[n][k]; // recursively find let ans = Math.abs(Gilbreath(mp, hash, n - 1, k + 1) - Gilbreath(mp, hash, n - 1, k)); // store the ans if (!mp.hasOwnProperty(n)) mp[n] = {}; mp[n][k] = ans; return ans; } // Print first n terms of Gilbreath sequence // successive absolute differences of primes // read by antidiagonals upwards. function solve(n) { let i = 0, j = 0, count = 0; // map to store the matrix // and hash to check if the // element is present or not let mp = {}; let hash = {}; // set the primes of first row set_primes(mp, hash, 100); while (count < n) { // print the Gilbreath number process.stdout.write(Gilbreath(mp, hash, i, j) + ", " ); // increase the count count++; // anti diagonal upwards i--; j++; if (i < 0) { i = j; j = 0; } } } // Driver code let n = 15; solve(n); // This code is contributed by phasing17 |
Output:
2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Reference: http://oeis.org/A036262
Please Login to comment...