Function Repository Resource:

BarningHallTree

Source Notebook

Generate primitive Pythagorean triples in a tree form using Fibonacci matrices

Contributed by: Shenghui Yang

ResourceFunction["BarningHallTree"][n]

creates an n level Barning-Hall tree with Fibonacci matrices as the nodes.

ResourceFunction["BarningHallTree"][n,"PythagoreanTriples"]

creates a tree with primitive Pythagorean triples as the nodes.

ResourceFunction["BarningHallTree"][n,"LabeledWithPythagoreanTriples"]

labels the Fibonacci matrices with corresponding primitive Pythagorean triples.

Details

The Fibonacci matrices in the nodes of a Barning-Hall tree are of the form .
Given the parent node , the three child nodes are , and .
The root node is defined by setting a=1 and b=1.
The corresponding Pythagorean triple for each node matrix is (b×d,2a×c,a×d+b×c).

Examples

Basic Examples (1) 

Create a two level Barning-Hall (B-H) tree with Fibonacci matrices as nodes:

In[1]:=
ResourceFunction["BarningHallTree"][1]
Out[1]=

Scope (1) 

Use "LabeledWithPythagoreanTriples" property to display the B-H tree together with its associated primitive Pythagorean triples:

In[2]:=
Tree[ResourceFunction["BarningHallTree"][2, "LabeledWithPythagoreanTriples"], TreeLayout -> "RadialEmbedding"]
Out[2]=

Applications (3) 

TreeExtract can be used to look up a certain node. To find a primitive Pythagorean triple containing 185, search for Fibonacci matrices that corresponding to a2+b2=1852 or a2+1852=c2 for some positive integer (a,b,c):

In[3]:=
data = ResourceFunction["BarningHallTree"][5, "PythagoreanTriples"]
Out[3]=
In[4]:=
TreePosition[data, _?(MemberQ[#, 185] &)]
Out[4]=

There are three valid solutions up to tree level 5:

In[5]:=
TreeData[TreeExtract[data, #]] & /@ %
Out[5]=

We can verify the solution with Reduce:

In[6]:=
{Reduce[a^2 + b^2 == 185^2 && a < b && GCD[a, b] == 1, {a, b}, PositiveIntegers],
 Reduce[a^2 + 185^2 == c^2 && a < c && GCD[a, c] == 1 && c < 1000, {a,
    c}, PositiveIntegers]}
Out[6]=

Properties and Relations (2) 

The zeroth tree node is the root node:

In[7]:=
ResourceFunction["BarningHallTree"][0]
Out[7]=

The zeroth tree node corresponds to the smallest Pythagorean triangle with legs 3 and 4 and hypotenuse 5:

In[8]:=
ResourceFunction[
 "BarningHallTree"][0, "LabeledWithPythagoreanTriples"]
Out[8]=

Possible Issues (1) 

A Barning-Hall tree grows exponentially. BarningHallTree contains a limit of 12 and returns unevaluated for larger values:

In[9]:=
ResourceFunction["BarningHallTree"][100]
Out[9]=

Neat Examples (7) 

Find Pythagorean triples (a,b,c) such that a<b<c and c-b=1. Some examples are (3,4,5) and (5,12,13). We can use the B-H tree function to find more:

In[10]:=
data = ResourceFunction["BarningHallTree"][8, "PythagoreanTriples"];
In[11]:=
pos = Reverse[
  Most[TreePosition[
    data, _?(With[{triple = NumericalSort[#]}, triple[[3]] - triple[[2]] == 1] &)]]]
Out[11]=

Find the corresponding Fibonacci matrices (notice that each matrix is the parent of matrix to the right):

In[12]:=
labeled = ResourceFunction["BarningHallTree"][8, "LabeledWithPythagoreanTriples"];
TreeData[TreeExtract[labeled, #]] & /@ pos
Out[13]=

Illustrate the path by highlighting the last four elements in the list above with LightYellow:

In[14]:=
Tree[TreeExtract[labeled, {3, 3, 3, 3, 3}], TreeLayout -> "RadialEmbedding", TreeElementStyle -> {{} -> LightYellow, {3} -> LightYellow, {3, 3} -> LightYellow, {3, 3, 3} -> LightYellow}]
Out[14]=

Directly generate Fibonacci matrices beginning with the same root node:

In[15]:=
Short[MatrixForm /@ (res = NestList[
     With[{a = #[[2, 1]], b = #[[1, 2]]}, {{a, b}, {a + b, 2 a + b}}] &, {{2, 1}, {3, 5}}, 100
     ])]
Out[15]=

Compute the triple for each matrix:

In[16]:=
Short[NumericalSort@*
   Function[{grid}, {2 grid[[1, 1]] grid[[2, 1]], grid[[1, 2]] grid[[2, 2]], grid[[1, 1]] grid[[2, 2]] + grid[[1, 2]] grid[[2, 1]]}] /@ res]
Out[16]=

Check that the results are correct by for a random triple from the list above:

In[17]:=
{20605 - 20604 == 1, 203^2 + 20604^2 == 20605^2}
Out[17]=

All hypotenuses in these triples are associated with the following OEIS sequence:

In[18]:=
ResourceFunction["OEISSequence"]["A001844"] // Short
Out[18]=

Publisher

Shenghui Yang

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 26 July 2024

Source Metadata

Related Resources

Author Notes

BH tree grows rapidly. The node for n-th level is 3n. I put a hard limit in the code to prevent generating too many levels for the tree.

License Information