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 i^{th} 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 = 10Output:2, 1, 3, 1, 2, 5, 1, 0, 2, 7,Input:n = 15Output: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 bywhere **n>0**,**F(0, k)**is the kth prime number where**n = 0**.

- 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 0^{th}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 0
^{th}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:

`// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,

**Reference**: http://oeis.org/A036262

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Goldbach's Weak Conjecture for Odd numbers
- Program to implement Collatz Conjecture
- Program for Goldbach’s Conjecture (Two Primes with given Sum)
- Lemoine's Conjecture
- Ramanujan–Nagell Conjecture
- Legendre's Conjecture
- Maximum Sequence Length | Collatz Conjecture
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to print a Hollow Triangle inside a Triangle
- Sum of all the numbers present at given level in Pascal's triangle
- Sum of all the numbers in the Nth row of the given triangle
- Sum of all the numbers present at given level in Modified Pascal’s triangle
- Sum of numbers in the Kth level of a Fibonacci triangle
- Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check whether a given point lies inside a triangle or not
- Pascal's Triangle
- Count Integral points inside a Triangle
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.