hey i really need help, how can i generate a simple coding of pythagorean table where it reads the upper right triangle? together, how to get table parameters, use nested loops (which prints out the raw list of all Pythagorean-candidates (do not need to store in an array) & how to extract the pythagorean numbers? i would really really apreciate it, thanks y'all very much for support

here's what i have as my pythagorean code for now

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
	int m, mm;
	double xm;
	int i, j;
	int c;

	//Get candidate-table dimensions
	cout << "This program generates Pythagorean candidate numbers in a table.\n\n";
	cout << "Type the absolute P-candidate maximum M:\n";
	cin  >> m;

	xm = m+1;
	mm = sqrt(xm/2.);
	cout << "The row/column size of the candidate table is: " << mm << "\n\n\n";

	for( j = 2; j < mm; j++ )
	{
		cout << setw(5) << j;
	}
	cout << "\n\n";

	//Generates table
	for( i = 1; i < mm - 1; i++ )
	{
		cout << "R= " << setw(2) << i;

		for( j = 2; j <= i; j++ )
		{
			c = (( i * i ) + ( j * j ));
			cout << setw(5) << c;
		}
		cout << "\n";
	}
	cout << endl;

	system( "Pause" );
	return 0;
}	//end main

but i need to display only the upper right side of the triangle and how to get my following questions

Recommended Answers

All 3 Replies

I've written a Pythagorean Number generator in C++Builder that could be adapted to other environments.

It uses an Edit box to accept the maximum length for the adjacent and opposite sides. It uses a second Edit box where you can input an angle for when you want your Pythagorean triplets to yield a triangle as close as possible so some specified angle.

It uses two List boxes. One holds all triplets where there is no common factors involved. The second list box shows triplets with common factors.

All of the code occurs in the Button1Click method.

void __fastcall TPythagoras::Button1Click(TObject *Sender)
{
int a, b, c, e, f, g, count1(0), count2(0);
int maxn(0), maxm;
double d, mangle, diff, bestd(91);
bool nofactors;
ListBox1->Clear();
ListBox2->Clear();
a = atoi(Edit1->Text.c_str());		// Maximum length of side in a triangle.
mangle = atof(Edit2->Text.c_str());		// Maximum length of side in a triangle.
b = 0;
while(++b <= a)
	{
   c = b;
   while(++c <= a)
   	{
      d = hypot(b, c);
      e = (int)d;
      if(d == e)		// d is an integer.
      	{			// Only add d to the list if there are no common factors.
         d = atan2(b, c)*180*M_1_PI;
         f = a/2 + 1;
         g = 1;
         nofactors = true;
         while(++g < f)
         	{
            if(b%g == 0 && c%g == 0 && e%g == 0)
            	{
            	nofactors = false;
               ListBox2->AddItem(AnsiString(b) + "   "
               										  + AnsiString(c)
                                               + "   "
                                               + AnsiString(e)
                                               + "   "
                                               + AnsiString(d)
                                               + "°", Sender);
               ++count2;
               break;
               }
            }
         if(nofactors)
         	{
	      	ListBox1->AddItem(AnsiString(b) + "   "
            										  + AnsiString(c)
                                            + "   "
                                            + AnsiString(e)
                                               + "   "
                                               + AnsiString(d)
                                               + "°", Sender);
            ++count1;
            diff = fabs(mangle - d);
            if(diff < bestd)
            	{
               bestd = diff;
            	maxn = b;
               maxm = c;
               }
            }
         }
      }
   }
Label2->Caption = AnsiString(count1) + " Pythagorean triplets.";
Label3->Caption = AnsiString(count2) + " with common factors.";
if(maxn)
	{
   d = atan2(maxn, maxm)*180*M_1_PI;
   e = hypot(maxn, maxm);
	Label4->Caption = "Closest to " + AnsiString(mangle)
   										  + "°    "
                                   + AnsiString(maxn)
                                   + ", " + AnsiString(maxm)
                                   + ", " + AnsiString(e)
                                   + "   " + AnsiString(d) + "°";
   }
}

This works very well and could be used as an assignment or embellished as a project.

Regards
Foamgum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.