Function Repository Resource:

BarningHallTreePosition

Source Notebook

Finds the position of a primitive Pythagorean triple in Barning-Hall tree

Contributed by: Shenghui Yang

ResourceFunction["BarningHallTreePosition"][triple]

finds the position of the primitive Pythagorean triple on the Barning-Hall tree.

ResourceFunction["BarningHallTreePosition"][triple,"PathWithPythagoreanTriple"]

creates a pruned tree from root node to the primitive Pythagorean triple on the Barning-Hall tree with intermediate Pythagorean triples.

ResourceFunction["BarningHallTreePosition"][triple,"PathWithFibonacciPythagoreanPair"]

also includes Fibonacci matrices in the labels.

Details

A primitive Pythagorean triple (a,b,c) is defined as a leg of odd length, leg of even length and hypotenuse of odd length in an integral right triangle. The greatest common divisor of the three numbers (a,b,c) is one.
The Fibonacci matrices in the nodes of the Barning-Hall tree are of the form .
Given the parent node , the three child nodes are in this order: , and .
The root node associated with (3,4,5) is and is defined by setting a=1 and b=1.
The corresponding Pythagorean triple for each node matrix is (a×c,2b×d,a×d+b×c).
The algorithm effectively uses a reverse formula of the Fibonacci matrix generation to find the parent node of the input iteratively.
The list returned by this function uses the same convention as TreePosition.

Examples

Basic Examples (2) 

Given a primitive Pythagorean triple, find its position on the Barning-Hall tree:

In[1]:=
ResourceFunction["BarningHallTreePosition"][{377, 336, 505}]
Out[2]=

See the tree:

In[3]:=
ResourceFunction[
 "BarningHallTreePosition"][{377, 336, 505}, "PathWithPythagoreanTriple"]
Out[3]=

Scope (2) 

Use "PathWithFibonacciPythagoreanPair" to show the tree withFibonacci matrices from the root node to the target:

In[4]:=
ppt = {377, 336, 505};
pos = ResourceFunction["BarningHallTreePosition"][ppt];
Tree[
 ResourceFunction["BarningHallTreePosition"][ppt, "PathWithFibonacciPythagoreanPair"],
 TreeElementStyle -> ((# -> LightBlue) & /@ FoldList[Flatten@{##} &, {}, pos])]
Out[5]=

Use "PathWithPythagoreanTriple" to show the tree from root node to the target with Fibonacci matrices:

In[6]:=
ppt = {377, 336, 505};
pos = ResourceFunction["BarningHallTreePosition"][ppt];
Tree[
 ResourceFunction["BarningHallTreePosition"][ppt, "PathWithPythagoreanTriple"],
 TreeElementStyle -> ((# -> LightBlue) & /@ FoldList[Flatten@{##} &, {}, pos])]
Out[8]=

Properties and Relations (3) 

BarningHallTreePosition with "PathWithFibonacciPythagoreanPair" automatically aligns the Pythagorean triples vertically in the node to minimize the width of each node as the triples grows rapidly:

In[9]:=
ppt = {22967, 11544, 25705};
pos = ResourceFunction["BarningHallTreePosition"][ppt];
Tree[ResourceFunction["BarningHallTreePosition"][ppt, "PathWithFibonacciPythagoreanPair"], TreeLayout -> Left, TreeElementStyle -> ((# -> LightBlue) & /@ FoldList[Flatten@{##} &, {}, pos])]
Out[11]=

"PathWithPythagoreanTriple" saves raw data therefore no automatic formatting implemented:

In[12]:=
ppt = {22967, 11544, 25705};
pos = ResourceFunction["BarningHallTreePosition"][ppt];
res = Tree[
  ResourceFunction["BarningHallTreePosition"][ppt, "PathWithPythagoreanTriple"], TreeLayout -> Left]
Out[14]=

Use TreeMap and Column or TableForm function to convert the tree into more compact and legible form:

In[15]:=
Tree[
 TreeMap[
  TableForm[Transpose[{#}], TableHeadings -> {{"a", "b", "c"}, None}, TableAlignments -> Right] &, res],
 TreeElementStyle -> ((# -> LightBlue) & /@ FoldList[Flatten@{##} &, {}, pos]),
 TreeLayout -> Left
 ]
Out[15]=

BarningHallTreePosition properly handles the display of gigantic Pythagorean triples with deep tree levels using the Format function internally. Together with TreeExtract and TreeElementStyle we can create a neat subtree. Specify a Pythagorean triple with huge values:

In[16]:=
ppt = {26950207347228004414681970231137606665817249155138706856300533016476330726999828367568284422474109418748588035001262760304173075860243357461569412025722384083536125998589, 8888556526768960014157162035870221196745067250642385391764741632505209907182507388964647770981124711868780593200186457330376730584950665065502122417952335302966588577100, 28378162611207748675165988067550742732874769560870018987955825881684565339244695903309632459713702107892780396696560300416946286189630518178835699721459975834724244843589};

Each number is close to 170 digits:

In[17]:=
Length[IntegerDigits[#]] & /@ ppt
Out[17]=

The path on the tree has 300 levels of depth and would not be a useful visualization:

In[18]:=
lastNLevels = 3;
pos4 = ResourceFunction["BarningHallTreePosition"][ppt];
{Length[pos4], Short[pos4]}
Out[20]=

Take the last three levels including the target input on the tree:

In[21]:=
res = TreeExtract[
   ResourceFunction["BarningHallTreePosition"][ppt, "PathWithFibonacciPythagoreanPair"], pos4[[;; -lastNLevels]]];

Customize the coordinate for the tree nodes to arrange the sibling, parents and grandparent node for the input:

In[22]:=
Tree[res,
 TreeElementStyle -> {pos4[[-lastNLevels + 1 ;;]] -> LightBlue},
 TreeElementCoordinates -> {
   {} -> {0, 1/3},
   {1} -> {1, 0}, {2} -> {1, 1/3}, {3} -> {1, 2/3},
   {1, 1} -> {2, -1/3}, {1, 2} -> {2, 0}, {1, 3} -> {2, 1/3}}]
Out[22]=

Possible Issues (3) 

The input must be Pythagorean triple. For instance {5,12,14} is not Pythagorean. The function returns unevaluated:

In[23]:=
ResourceFunction["BarningHallTreePosition"][{5, 12, 14}]
Out[23]=

The Pythagorean triple must be primitive. For instance {5,12,13} with GCD[5,12,13] = 1. {10,24,26} is Pythagorean but not primitive. The input yields unevaluated form:

In[24]:=
{ResourceFunction["BarningHallTreePosition"][{5, 12, 13}], ResourceFunction["BarningHallTreePosition"][2*{5, 12, 13}]}
Out[24]=

The function uses canonical primitive Pythagorean triple notation as it is compatible with its intermediate triples. Changing the sequence of the input results in an unevaluated result:

In[25]:=
{ResourceFunction["BarningHallTreePosition"][{5, 12, 13}], ResourceFunction["BarningHallTreePosition"][{5, 13, 12}]}
Out[25]=

Neat Examples (8) 

Schinzel’s hypothesis H implies that there are infinite number of primitive Pythagorean triples with two primes. This requires that both odd leg a and odd hypotenuse being prime number and the length of the even leg is one less than that of the hypotenuse. Let's take a look at the example:

In[26]:=
(* https://oeis.org/A048161 *)
a = 1901;
c = (a^2 + 1)/2;
{c, PrimeQ[c], PrimeQ[a]}
Out[27]=

Solve for the Pythagorean equation and notice that b is one less than c:

In[28]:=
Reduce[a^2 + b^2 == c^2 && Mod[b, 2] == 0, {b}, PositiveIntegers]
Out[28]=

Actually, given such c, the other two variables are determined in this Diophantine equation:

In[29]:=
Reduce[x^2 + y^2 == 1806901^2 && Mod[y, 2] == 0 && x \[Element] Primes, {x, y}, PositiveIntegers]
Out[29]=

For other cases, we choose prime p such that is also a prime number:

In[30]:=
candidates = Select[Prime[Range[120]], PrimeQ[(#^2 + 1)/2] &];
trps = Module[{a = #, c}, c = (a^2 + 1)/2; {a, c - 1, c}] & /@ candidates;

Pick 5 elements and we can check the location of the triples on the tree with obvious pattern:

In[31]:=
ResourceFunction["NiceGrid"][MapThread[
  <|"Primitive Pythagorean Triple" -> #1, "Position" -> #2|> &,
  {Rest[trps[[;; 6]]], Rest[ResourceFunction["BarningHallTreePosition"][#] & /@ trps][[;; 5]]}]]
Out[31]=

For instance, we can visualize the {11,60,61} on the B-H tree:

In[32]:=
pos = {3, 3, 3, 3};
pathLoc = (# -> LightBlue) & /@ FoldList[Flatten[{##}] &, {}, Rest[pos]];
Tree[ResourceFunction[
  "BarningHallTreePosition"][\!\(TraditionalForm\`{11, 60, 61}\), "PathWithFibonacciPythagoreanPair"],
 TreeElementStyle -> {Splice@pathLoc, pos -> LightYellow}]
Out[33]=

The number of 3's in each position for primitive Pythagorean triple with two primes defines an interesting sequence. The sequence is A068501 with each term minus one:

In[34]:=
data = Length /@ Rest[ResourceFunction["BarningHallTreePosition"][#] & /@ trps];
Column@{data, ResourceFunction[
ResourceObject[<|"Name" -> "OEISSequence", "ShortName" -> "OEISSequence", "UUID" -> "12010764-9f22-4e11-9ecf-3310f9f9e7d4", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Return the list provided by the OEIS for a given OEIS sequence number", "RepositoryLocation" -> URL[
         "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$83e5783b1cb54fb8a9f1819c50935826`OEISSequence", "FunctionLocation" -> CloudObject[
         "https://www.wolframcloud.com/obj/bb406b01-d308-49cc-9402-42e3af604c25"]|>, ResourceSystemBase -> Automatic]]["A068501"][[
    2 ;; 25]] - 1}
Out[35]=

Plot the curve of the sequence:

In[36]:=
ListPlot[data, Filling -> Bottom]
Out[36]=

Notice that the tree position of repeating 3's is just a necessary condition for Pythagorean triple to have 2 primes. (7,24,25) is at {3,3} and 25 is not prime. In fact, the tree position of repeating 3's is sufficient and necessary condition for primitive Pythagorean triple with (a,c-1,c) form in our Barning-Hall tree convention. Those triples with two primes are strictly in the subset of the (a,c-1,c) cases.

Publisher

Shenghui Yang

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 30 July 2024

Source Metadata

Related Resources

License Information