Triangle of numbers arising from Gilbreath 's conjecture | Python3 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
import math NEW_LINE def is_Prime ( n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def set_primes ( mp , hash , c ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 2 NEW_LINE while ( count < c ) : NEW_LINE INDENT if ( is_Prime ( i ) ) : NEW_LINE INDENT if 0 not in mp : NEW_LINE INDENT mp [ 0 ] = dict ( ) NEW_LINE DEDENT mp [ 0 ] [ count ] = i ; NEW_LINE count += 1 NEW_LINE if 0 not in hash : NEW_LINE INDENT hash [ 0 ] = dict ( ) NEW_LINE DEDENT hash [ 0 ] [ count - 1 ] = 1 ; NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def Gilbreath ( mp , hash , n , k ) : NEW_LINE INDENT if ( n in hash and k in hash [ n ] and hash [ n ] [ k ] != 0 ) : NEW_LINE INDENT return mp [ n ] [ k ] ; NEW_LINE DEDENT ans = abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; NEW_LINE if n not in mp : NEW_LINE INDENT mp [ n ] = dict ( ) NEW_LINE DEDENT mp [ n ] [ k ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT def solve ( n ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE count = 0 ; NEW_LINE mp = dict ( ) NEW_LINE hash = dict ( ) NEW_LINE set_primes ( mp , hash , 100 ) ; NEW_LINE while ( count < n ) : NEW_LINE INDENT print ( Gilbreath ( mp , hash , i , j ) , end = ' , ▁ ' ) NEW_LINE count += 1 NEW_LINE i -= 1 NEW_LINE j += 1 NEW_LINE if ( i < 0 ) : NEW_LINE INDENT i = j ; NEW_LINE j = 0 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 ; NEW_LINE solve ( n ) ; NEW_LINE DEDENT