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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } 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 ; } } } 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 ] ; int ans = abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; mp [ n ] [ k ] = ans ; return ans ; } void solve ( int n ) { int i = 0 , j = 0 , count = 0 ; map < int , map < int , int > > mp , hash ; set_primes ( mp , hash , 100 ) ; while ( count < n ) { cout << Gilbreath ( mp , hash , i , j ) << " , ▁ " ; count ++ ; i -- ; j ++ ; if ( i < 0 ) { i = j ; j = 0 ; } } } int main ( ) { int n = 15 ; solve ( n ) ; return 0 ; }