The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::PlanePath::MathImagePythagoreanTree -- primitive pythagorean triples by tree

SYNOPSIS

 use Math::PlanePath::MathImagePythagoreanTree;
 my $path = Math::PlanePath::MathImagePythagoreanTree->new;
 my ($x, $y) = $path->n_to_xy (123);

DESCRIPTION

In progress.

This path enumerates primitive Pythagorean triples in a breadth-first traversal of a ternary tree, either the "FB" Fibonacci boxes tree by H. Lee Price, or the "UAD" tree of Berggren, Banning, Hall and others.

Each point is an integer X,Y with an integer hypotenuse X^2+Y^2=Z^2. A primitive triple is one where X and Y have no common factor, which also means one of X,Y odd and the other even, and has Z always odd.

UAD Tree

   Y=40 |          14
        |
        |
        |
        |                                              7
   Y=24 |        5
        |
   Y=20 |                      3
        |
   Y=12 |      2                             13
        |
        |                4
    Y=4 |    1
        |
        +--------------------------------------------------
           X=3         X=15  X=20           X=35      X=45

The first point N=1 is at X=3,Y=4 which is the well-known triple 3^2+4^2=5^2. From it three further points N=2,3,4 are derived, then three more from each of those, etc, in a ternary tree.

     N=1      N=2..4      N=5..13   ...

                      +-> 7,24
          +-> 5,12  --+-> 55,48
          |           +-> 45,28
          |
          |           +-> 39,80
    3,4 --+-> 21,20 --+-> 119,120
          |           +-> 77,36
          |
          |           +-> 33,56
          +-> 15,8  --+-> 65,72
                      +-> 35,12

The middle path at each node, 20,21 then 119,120, etc, is all the triples with legs differing by 1.

The lower path at each node, 15,8 then 35,12 etc, is the primitives among a sequence of triples known to the ancient Babylonians,

     A=k^2-1, B=2*k, C=k^2+1

FB Tree

The FB tree is based on rearrangements of certain "Fibonacci boxes". The X,Y points reached are in a different sequence and a tree structure.

    Y=40 |         5
         |
         |
         |
         |                                             17
    Y=24 |       4
         |
         |                     8
         |
    Y=12 |     2                             6
         |
         |               3
    Y=4  |   1
         |
         +----------------------------------------------
           X=3         X=15   x=21         X=35

The first point N=1 is again at X=3,Y=4 and three further points N=2,3,4 are derived, then three more from each of those, etc.

     N=1      N=2..4      N=5..13   ...

                      +-> 9,40
          +-> 5,12  --+-> 35,12
          |           +-> 11,60
          |
          |           +-> 21,20
    3,4 --+-> 15,8  --+-> 55,48
          |           +-> 39,80
          |
          |           +-> 13,84
          +-> 7,24  --+-> 63,16
                      +-> 15,112

PQ Coordinates

Any Pythagorean triple can be parameterized as follows, taking A odd and B even,

    A = u^2 - v^2,  B = 2*u*v,  C = u^2 + v^2

    u = sqrt((C+A)/2),  v = sqrt((C-A)/2)

The starting point A=3,B=4 is then u=2,v=1.

The coordinates option on the path gives u,v as the returned X,Y values,

    my $path = Math::PlanePath::MathImagePythagoreanTree-E<gt>new
                  (coordinates => 'PQ');
    my ($u,$v) = $path->n_to_xy(1);  # u=2,v=1

FUNCTIONS

$path = Math::PlanePath::MathImagePythagoreanTree->new ()

Create and return a new path object.

($x,$y) = $path->n_to_xy ($n)

Return the X,Y coordinates of point number $n on the path. Points begin at 0 and if $n < 0 then the return is an empty list.

Fractional positions give an X,Y position along a straight line between the integer positions. Integer positions are always just 1 apart either horizontally or vertically, so the effect is that the fraction part appears either added to or subtracted from X or Y.

SEE ALSO

Math::PlanePath

HOME PAGE

http://user42.tuxfamily.org/math-image/index.html

LICENSE

Math-Image is Copyright 2010, 2011 Kevin Ryde

Math-Image is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-Image is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-Image. If not, see <http://www.gnu.org/licenses/>.