# Triangle of numbers arising from Gilbreath’s conjecture

• Last Updated : 08 Jun, 2021

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:

1. 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)|
2. 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.
3. 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.
4. 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 ``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[count++] = i;``            ``hash[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> mp,``                         ``HashMap> 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());``        ``}`` ` `        ``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());``        ``}`` ` `        ``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> mp,``                       ``HashMap> 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());``    ``}``    ``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> mp =``      ``new` `HashMap>();``    ``HashMap> hash = ``      ``new` `HashMap>();`` ` `    ``// 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 = ``new` `Dictionary<``int``,``int``>();``                ``}``                 ` `                ``if``(!mp.ContainsKey(count))``                ``{``                    ``mp.Add(count, i);``                ``}``                ``else``                ``{``                    ``mp[count] = i;``                ``}``                ``count++;``                 ` `                ``if``(!hash.ContainsKey(0))``                ``{``                    ``hash = ``new` `Dictionary<``int``,``int``>();``                ``}``                 ` `                ``if``(!hash.ContainsKey(count - 1))``                ``{``                    ``hash.Add(count - 1, 1);``                ``}``                ``else``                ``{``                    ``hash[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.`
Output:
`2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,`

Reference: http://oeis.org/A036262

My Personal Notes arrow_drop_up