Triangle of numbers arising from Gilbreath 's conjecture | C # code for printing the Triangle of numbers arising from Gilbreath 's conjecture ; Check whether the number is prime or not ; Set the 0 th row of the matrix with c primes from 0 , 0 to 0 , c - 1 ; Find the n , k term of matrix of Gilbreath 's conjecture ; recursively find ; store the ans ; Print first n terms of Gilbreath sequence successive absolute differences of primes read by antidiagonals upwards . ; map to store the matrix and hash to check if the element is present or not ; set the primes of first row ; print the Gilbreath number ; increase the count ; anti diagonal upwards ; Driver code
using System ; using System . Collections . Generic ; class GFG { 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 ; } 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 ; } } } } 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 ] ; int ans = Math . Abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; if ( ! mp . ContainsKey ( n ) ) { mp [ n ] = new Dictionary < int , int > ( ) ; } mp [ n ] [ k ] = ans ; return ans ; } static void solve ( int n ) { int i = 0 , j = 0 , count = 0 ; 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_primes ( mp , hash , 100 ) ; while ( count < n ) { Console . Write ( Gilbreath ( mp , hash , i , j ) + " , ▁ " ) ; count ++ ; i -- ; j ++ ; if ( i < 0 ) { i = j ; j = 0 ; } } } static void Main ( ) { int n = 15 ; solve ( n ) ; } }