1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#!/usr/bin/perl -w
# Copyright 2011, 2012, 2013 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath 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-PlanePath 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-PlanePath. If not, see <http://www.gnu.org/licenses/>.
# Usage: perl rationals-tree.pl
#
# Print the RationalsTree paths in tree form.
#
use 5.004;
use strict;
use List::Util 'max';
use Math::PlanePath::RationalsTree;
use Math::PlanePath::FractionsTree;
sub print_as_fractions {
my ($path) = @_;
my $n = $path->n_start;
foreach (1) {
my ($x,$y) = $path->n_to_xy($n++);
print centre("$x/$y",64);
}
print "\n";
print " /------------- -------------\\\n";
foreach (1 .. 2) {
my ($x,$y) = $path->n_to_xy($n++);
print centre("$x/$y",32);
}
print "\n";
print " /---- ----\\ /---- ----\\\n";
foreach (1 .. 4) {
my ($x,$y) = $path->n_to_xy($n++);
print centre("$x/$y",16);
}
print "\n";
print " / \\ / \\ / \\ / \\\n";
foreach (1 .. 8) {
my ($x,$y) = $path->n_to_xy($n++);
print centre("$x/$y",8);
}
print "\n";
print " / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\\n";
foreach (16 .. 31) {
my ($x,$y) = $path->n_to_xy($n++);
print centre("$x/$y",4);
}
print "\n";
print "\n";
}
sub centre {
my ($str, $width) = @_;
my $extra = max (0, $width - length($str));
my $left = int($extra/2);
my $right = $extra - $left;
return ' 'x$left . $str . ' 'x$right;
}
sub xy_to_cfrac_str {
my ($x,$y) = @_;
my @quotients;
while ($x > 0 && $y > 0) {
push @quotients, int($x/$y);
$x %= $y;
($x,$y) = ($y,$x);
}
return "[".join(',',@quotients)."]";
}
sub print_as_cfracs {
my ($path) = @_;
my $n = $path->n_start;
foreach (1) {
my ($x,$y) = $path->n_to_xy($n++);
print centre(xy_to_cfrac_str($x,$y), 72);
}
print "\n";
print " /--------------- ---------------\\\n";
foreach (1 .. 2) {
my ($x,$y) = $path->n_to_xy($n++);
print centre(xy_to_cfrac_str($x,$y), 36);
}
print "\n";
print " /----- -----\\ /----- -----\\\n";
foreach (1 .. 4) {
my ($x,$y) = $path->n_to_xy($n++);
print centre(xy_to_cfrac_str($x,$y), 18);
}
print "\n";
print " / \\ / \\ / \\ / \\\n";
foreach (1 .. 8) {
my ($x,$y) = $path->n_to_xy($n++);
print centre(xy_to_cfrac_str($x,$y), 9);
}
print "\n";
print "\n";
}
#------------------------------------------------------------------------------
my $rationals_type_arrayref
= Math::PlanePath::RationalsTree->parameter_info_hash()->{'tree_type'}->{'choices'};
my $fractions_type_arrayref
= Math::PlanePath::FractionsTree->parameter_info_hash()->{'tree_type'}->{'choices'};
print "RationalsTree\n";
print "-------------\n\n";
foreach my $tree_type (@$rationals_type_arrayref) {
print "$tree_type tree\n";
my $path = Math::PlanePath::RationalsTree->new
(tree_type => $tree_type);
print_as_fractions ($path);
}
print "\n";
print "FractionsTree\n";
print "-------------\n\n";
foreach my $tree_type (@$fractions_type_arrayref) {
print "$tree_type tree\n";
my $path = Math::PlanePath::FractionsTree->new
(tree_type => $tree_type);
print_as_fractions ($path);
}
print "\n";
print "-----------------------------------------------------------------------\n";
print "Or written as continued fraction quotients.\n";
print "\n";
print "RationalsTree\n";
print "-------------\n\n";
foreach my $tree_type (@$rationals_type_arrayref) {
print "$tree_type tree\n";
my $path = Math::PlanePath::RationalsTree->new
(tree_type => $tree_type);
print_as_cfracs ($path);
}
print "\n";
print "FractionsTree\n";
print "-------------\n\n";
foreach my $tree_type (@$fractions_type_arrayref) {
print "$tree_type tree\n";
my $path = Math::PlanePath::FractionsTree->new
(tree_type => $tree_type);
print_as_cfracs ($path);
}
exit 0;
|