#!/usr/bin/perl

# No copyright. Use at your own risk !!!! You have been warned.......
# Vinyasi, http://mayashastra.org/webmaster, 6:53 PM 10/9/02

# Perl program for generating web-ready tables of polygon's apex angles, their cosines, and
# their polynomial constructions of integer coefficients for a portion of two different
# infinite series. The output from running this file may then be inserted into a web-page
# for internet viewing. Without modifying this program's code, the default option will
# produce tables of polygon-stats for the first three Fermat primes (3, 5, and 17). The
# second option is selected at "SEE HERE" by commenting and uncommenting some lines of code.
# This will produce similar tables, but for a few odd-sided polygons rather than just
# 'Fermats'.

# NOTE:
# Tables for the following polygons will be produced if
# $upper_limit_for_fermat_primed_polygons is set to:
# 0 => Triangle,
# 1 => Triangle and Pentagon,
# 2 => Triangle, Pentagon, and Heptadecagon (17-gon),
# 3 => Triangle, Pentagon, Heptadecagon (17-gon), and 257-gon,
# 4 => Triangle, Pentagon, Heptadecagon (17-gon), 257-gon, and 65'537-gon (Ouch !!!),
# etc....

# line 25

#________________________________________________________________________________
# IF YOU ARE GOING TO GENERATE POLYNOMIALS:
# THEN DO NOT SET $upper_limit_for_fermat_primed_polygons LARGER THAN 2
# OR $upper_limit_for_odd_sided_golden_polygons LARGER THAN TWO DIGITS LONG
# UNLESS YOUR PC HAS GIGA-, OR MAYBE TRILO-, BYTES OF RAM WHICH INCLUDES THE
# SIZE OF ITS PAGE / SWAP FILE ON YOUR HARD DRIVE (for Windows, it's: WIN386.SWP),
# OR YOUR SWAP PARTITION WITHIN LINUX OR UNIX !!!!!!

# Do you want to generate polynomials of each polygon's Phi proportions?
 $polynomial = "yes";
# $polynomial = "no";

# DONT MIX THESE UP !!!!! IF $polynomial equals "yes", THEN
# $upper_limit_for_fermat_primed_polygons MUST BE VERY, VERY SMALL.
# $upper_limit_for_odd_sided_golden_polygons MUST BE GREATER THAN 2 AND LESS THAN
# TWO DIGITS LONG. NO MORE.
$upper_limit_for_fermat_primed_polygons = 2;
$upper_limit_for_odd_sided_golden_polygons = 15;

# IF YOU ARE --NOT-- GOING TO GENERATE POLYNOMIALS, THEN I DON'T KNOW WHAT THE
# SAFE LIMIT IS. THIS PROGRAM WILL PREVENT YOU FROM GOING BEYOND SAFE LIMITS
# IF YOU ARE GENERATING POLYNOMIALS. WITHOUT POLYNOMIALS, YOU ARE ON YOUR OWN !!!
#________________________________________________________________________________

# Lower limit for both fermat and odd-sided polygons:
$start = 3;

# line 54

# Do you want to test the polynomials that are generated by this program for their
# coefficient's correctness with a randomly chosen root? This test only runs if 
# $polynomial is also set to "yes".
 $test = "yes";
# $test = "no";

# Do you want to use trigonometry's sine or cosine functions?
# Not that it might matter --- but you never know.......
$trig = "Sine";
# $trig = "Cosine";

# Filepath to the Minus/Plus GIF image:
$minus_plus = "minus_plus.gif";

# Does your computer use a carriage return followed by a line feed (\r\n) to break a line? [Such
# as on Unix]
# $car_line = "\015\012";
# Or does it merely use a line feed (\n)? [Such as on Windows...]
 $car_line = "\012";
# Macintosh is one of the above.....

# This next line of code (srand) isn't necessary with newer versions of Perl,
# from 5.004 onward.
# srand (time ^ ($$ + ($$ << 15))) if($test eq "yes");

# This next line will round most values to this many digits.
# On one value, the Circumferencial Angle, it will truncate.
$accuracy = 10;

# line 85

# These two lines are constants. Don't mess with them unless you have better ideas....
$prime = "";
use constant PI => 4 * atan2 1, 1;

unless( open( OUT, ">out.html" ) ) {
	print "OOPS !!! Can't open data file to save these tables to."; }

# "SEE HERE"
# Uncomment the following block of 6 lines of code and .....
# for($x1 = $start; $x1 <= $upper_limit_for_odd_sided_golden_polygons; $x1+=2) {
#	$prime = "Prime ";
#	for($x2 = 2; $x2 < ($x1 / 2); $x2++) {
#		if($x1 % $x2 == 0) {
#			push(@factors, $x2);
#			$prime = "Composite "; }}

# .... and comment out the following 2 lines of code for producing a portion
# of the infinite series of odd-sided, golden polygons and their associated polynomials.
# Polygons of Fermat-prime number of sides is a small slice of the infinite series of
# odd=sided golden polygons.
 for($x = 0; $x <= $upper_limit_for_fermat_primed_polygons; $x++) {
	$x1 = (2 ** (2 ** $x)) + 1;

# line 110

# Nothing else needs to be modified below this line unless you want to fool around.	:-)
#__________________________________________________________________________________

	$round = 10 ** $accuracy;
	$safe_fermat = 2;
	$safe_golden = 99;
	$safe_digits = length($safe_golden);

	if(($upper_limit_for_fermat_primed_polygons > $safe_fermat || $upper_limit_for_odd_sided_golden_polygons > $safe_golden) && $polynomial eq "yes") {
		print "\n\nARE YOU SURE YOU WANT TO DO THIS ???";
		print "\n\nYOU HAVE SET TOO HIGH A LIMIT !!!!\n\n";
		print "\$upper_limit_for_fermat_primed_polygons = ", $upper_limit_for_fermat_primed_polygons, "\n\nTHIS IS GREATER THAN\n\nTHE SAFE LIMIT OF ", $safe_fermat, " !!!!\n\n" if($upper_limit_for_fermat_primed_polygons > $safe_fermat);
		print "\$upper_limit_for_odd_sided_golden_polygons = ", $upper_limit_for_odd_sided_golden_polygons, "\n\nTHIS IS GREATER THAN\n\nTHE SAFE LIMIT OF ", $safe_digits, " DIGITS LONG !!!!\n\n" if($upper_limit_for_odd_sided_golden_polygons > $safe_golden);
		exit; }

# line 127

	$tot_sides = $x1;
	$tot_ang = ($tot_sides - 2) * 180;
	print OUT $car_line, "<br>&nbsp;<hr>&nbsp;<br>", $car_line x 2, $prime, $tot_sides, "-Gon, TotAng = ", $tot_ang, "&deg;";
	$num_ang = ($tot_sides - 1) / 2;
	$div = $tot_sides - 2;
	$sing_ang = int($round * $tot_ang / $tot_sides) / $round;
	$ang = $sing_ang / $div;
	print OUT " &nbsp; The circumferencial angle is ", $sing_ang, "&deg;", $car_line;
	$num_facs = @factors;
	print OUT "<br>The ", $tot_sides, "-Gon has its own unique roots, plus those of the:", $car_line if($num_facs > 0);
	for($fac = 0; $fac < $num_facs; $fac++) {
		print OUT "," if($fac > 0);
		print OUT " and" if($fac == ($num_facs - 1) && $num_facs > 1);
		print OUT " ", $factors[$fac], "-Gon"; }
	print OUT ".", $car_line if($num_facs > 0);
	print OUT $car_line, "<table border='0' cellpadding='0'><tr><td valign='middle'>", $car_line;

# line 146

	$const = -1;
	$alt_sign = -1;
	for($x3 = 1; $x3 <= $div; $x3 += 2) {
		$angle = $ang * $x3;
		$alt_sign *= $const;
		if($trig eq "Sine") {
			$sin_or_cos = sin(PI * $angle / 360) * 2 * $alt_sign;
		}else{
			$sin_or_cos = sqrt(2 - 2 * cos(PI * $angle / 180)) * $alt_sign; }
		$reciprocal = $sin_or_cos ** (-1);
		push(@root, (-1 * $sin_or_cos)) if($polynomial eq "yes");
		($sin_or_cos < 0) ? ($sign = " <img src='$minus_plus' alt='Minus or Plus' width='9' height='11' border='0' align='bottom'> ") : ($sign = " &#177; ");

# line 161

		$mod_ang = (($tot_ang * $x3) % ($tot_sides * $div)) / $div ;
		$int_ang = int(($tot_ang * $x3) / ($tot_sides * $div));
		$display = 10 * int($round * (abs($sin_or_cos) + 5 / $round) / 10) / $round;
		$reciprocal = 10 * int($round * (abs($reciprocal) + 5 / $round) / 10) / $round;
		$seq = ($x3 - 1) / 2 + 1;

		print OUT $seq, ". Angle is&nbsp; ", $int_ang;
		print OUT " ", $mod_ang, "/", $x1 if($mod_ang != 0);
		print OUT "&deg;", $car_line, "</td><td valign='middle'>", $car_line, "&nbsp;&nbsp;&nbsp;&nbsp;";
		print OUT " its ", $trig, " is", $car_line, "</td><td valign='middle'>", $car_line;
		print OUT "&nbsp; <font size='+2' face='Symbol'>&#102;</font>";
		print OUT "&nbsp; =&nbsp; ", $sign, $display, $car_line;
		print OUT "</td><td valign='middle'>", $car_line;
		print OUT "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; its Reciprocal is", $car_line, "&nbsp; 1 <big>&#247;</big> ";
		print OUT "<font size='+2' face='Symbol'>&#102;</font>", $car_line;
		print OUT "</td><td valign='middle'>", $car_line, "&nbsp;=";
		print OUT "&nbsp;", $sign, $reciprocal, $car_line;
		print OUT $car_line, "</td></tr><tr><td valign='middle'>", $car_line x 2 if($x3 != $div); }
	print OUT "</td></tr></table>", $car_line x 2;

# line 183

	if($polynomial eq "yes") {
		$coef[0] = 1;
		$coef[1] = $root[0];
		$coef[2] = 0;
		for($x4 = 1; $x4 < $num_ang; $x4++) {
			$coef[$x4 + 1] = $coef[$x4] * $root[$x4];
			for($x5 = 2; $x5 <= $x4; $x5++) {
				$temp[$x5] = $coef[$x5 - 1] * $root[$x4] + $coef[$x5]; }
			for($x5 = 2; $x5 <= $x4; $x5++) {
				$coef[$x5] = $temp[$x5]; }
			$coef[1] += $root[$x4]; }

# line 197

		for($x6 = 0; $x6 < 2; $x6++) {
			if($x6 == 0) {
				$recipe = "";
				$choose = rand($num_ang);
				$test_this = -1 * $root[$choose];
				$summate = 0;
				$count += 2;
			}else{
				$recipe = " Reciprocal"; }
			print OUT "<p>Its", $recipe, " Polynomial of All", $recipe, " <font size='+2' face='Symbol'>&#102;</font> Roots is", $car_line, "<br><dd>", $car_line;
			for($x7 = 0; $x7 <= $num_ang; $x7++) {
				$summate += $coef[$x7] * $test_this ** ($num_ang - $x7) if($x6 == 0);
				if($x7 % 2 == 1) {
					if((($x7 + 3 + $count) / 2) % 2 == 1) {
						$sign = " &nbsp;<img src='$minus_plus' alt='Minus or Plus' width='9' height='11' border='0' align='bottom'>&nbsp; ";
					}else{
						$sign = " &nbsp;&#177;&nbsp; "; }
# line 216
				}elsif($coef[$x7] < 0) {
					$sign = " &nbsp;&#150;&nbsp; ";
				}else{
					$sign = " &nbsp;+&nbsp; "; }
				$sign = "" if($x7 == 0);
				if($x6 == 0) {
					$coeff = 10 * int($round * (abs($coef[$x7]) + 5 / $round) / 10) / $round;
				}else{
					$coeff = 10 * int($round * (abs($coef[$num_ang - $x7]) + 5 / $round) / 10) / $round; }
				$coeff = "" if($coeff == 1 && $num_ang != $x7);
				print OUT $sign, $coeff;
				print OUT "x" if($num_ang != $x7);
				print OUT "<sup>", ($num_ang - $x7), "</sup>" if($num_ang - $x7 != 1 && $num_ang != $x7); }
		print OUT " = 0", $car_line x 2; }

# line 232

		undef @factors;
		undef @root;
		undef @coef;
		undef @temp;

		if($test eq "yes") {
			print OUT "<p>", $car_line, "Test result for this polynomial ", $car_line;
			print OUT "<font size='+1' face='Symbol'>&#174;</font> ", $car_line;
			print OUT "If <big>&nbsp;", $summate, "&nbsp;</big> loosely equals zero, ", $car_line;
			print OUT "then this test proves positive.", $car_line; }}}

if($test eq "yes" && $polynomial eq "yes") {
	print OUT "<p>", $car_line x 2, "If the test result is a number in scientific ", $car_line;
	print OUT "notation showing some absolute value less than ", $car_line;
	print OUT $accuracy, " decimal digits, then this isn't too bad. ", $car_line;
	print OUT "In fact, it's pretty good since this program ", $car_line;
	print OUT "rounds to ", $accuracy, " digits.", $car_line; }

print OUT $car_line, "<br>&nbsp;<hr>&nbsp;<br>", $car_line x 2;

# line 254

close(OUT);

exit;

#________________________________________________________________________________________

# These three lines of code, or something similar, may be useful for debugging. Paste them
# anywhere to discover what the program is up to at that point in its processing:

#print $car_line, "num_ang=", $num_ang, " x4=", $x4, " coef-x4=", $coef[$x4], " x5=", $x5, " x6=", $x6, " x7=", $x7, " root-x5=", root[$x5], " temp-x5=", $temp[$x5];
#print $car_line;
#exit;
